This commit is contained in:
2025-03-26 12:41:58 -04:00
parent 637b26a0e9
commit 416dd617e6
36 changed files with 732 additions and 0 deletions

1
example_7/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_7/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_7"
version = "0.1.0"
edition = "2021"
[dependencies]

27
example_7/src/main.rs Normal file
View 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);
}