Add code
This commit is contained in:
1
example_7/.gitignore
vendored
Normal file
1
example_7/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
6
example_7/Cargo.toml
Normal file
6
example_7/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "example_7"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
27
example_7/src/main.rs
Normal file
27
example_7/src/main.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
fn double_free() {
|
||||
// every value has an owner, responsible for destructor (RAII).
|
||||
// compiler checks:
|
||||
|
||||
// only ever one owner:
|
||||
// no double-free
|
||||
|
||||
let x: Vec<u32> = Vec::new();
|
||||
let y = x;
|
||||
drop(x); // illegal, y is now owner
|
||||
}
|
||||
|
||||
fn main() {
|
||||
double_free();
|
||||
immutable();
|
||||
}
|
||||
|
||||
// Functions and structs can be declared any order (looking at you c++ and python!)
|
||||
fn immutable() {
|
||||
let v = Vec::new();
|
||||
|
||||
// this compiles just fine:
|
||||
println!("len: {}", v.len());
|
||||
|
||||
// this will not compile; would need mutable access
|
||||
v.push(42);
|
||||
}
|
||||
Reference in New Issue
Block a user