of immutable variable `a` mutability.rs:6 a += 1; ^~~~~~ mutability.rs:2:9: 2:10 note: prior assignment occurs here mutability.rs:2 let a = 1; ^ error: aborting due to previous error 8
of moved value: `a` ownership.rs:8 println!("a: {}", a); ^ ownership.rs:7:14: 7:15 note: `a` moved here because it has type `collections::string::String`, which is non-copyable ownership.rs:7 my_print(a); ^ error: aborting due to previous error 10
ownership here println!("s: {}", s); // so this is implied // free(s); } fn main() { let a = "test".to_string(); my_print(a); println!("a: {}", a); } 11
4 Things can be "boxed" or heap allocated fn my_print(b: Box<i32>) { println!("b: {}", b); // this is implied // free(b); } fn main() { let a = Box::new(5); // let a = box 5; my_print(a); // this would be an moved/ownership error // println!("a: {}", a); } 13
the lifetime of objects 4 Not possible to use a pointer after it is freed 4 Also not possible to use uninitialized memory 4 Heap memory is freed automatically, no GC! 4 All enforced by the compiler 14
create simd vectors let x = f32x4(1.0, 2.0, 3.0, 4.0); let y = f32x4(4.0, 3.0, 2.0, 1.0); // simd product let z = x * y; // like any struct, the simd vector can be destructured let f32x4(a, b, c, d) = z; println!("z: {}", (a, b, c, d)); } 22
upcoming 1.0 release 4 Ownership system is complicated 4 Compiler is smart, but not forgiving 4 Built-in test framework can be annoying 4 Easy to get confused (ex: two types of strings) 28