Illustrations
The following illustrations are from Programming Rust and The Rust Book.
A vector
#![allow(unused)] fn main() { let padovan = vec![1, 1, 1, 2, 2, 3, 4, 5, 7, 9]; }
A box and a string
#![allow(unused)] fn main() { let point = Box::new((0.625, 0.5)); let label = format!("{:?}", point); }
A vector containing instances of a struct
#![allow(unused)] fn main() { struct Person { name : String, birth : i32 } let mut composers = Vec::new(); composers.push(Person { name : "Palenstrina".to_string(), birth: 1525 }); composers.push(Person { name : "Dowland".to_string(), birth: 1563 }); composers.push(Person { name : "Lully".to_string(), birth: 1632 }); }
A string and a ref to it
#![allow(unused)] fn main() { let s1 = String::new("hello"); let s = &s1; }
s
stores the address of s1
.
A vec containing strings
#![allow(unused)] fn main() { let s = vec!["udon".to_string(), "ramen".to_string(), "soba".to_string()]; let t = s; }
s
in memory
The result of assigning s
to t
Assigning a String moves the value, whereas assigning an i32 copies it
#![allow(unused)] fn main() { let string1 = "somnambulance".to_string(); let string2 = string1; let num1 : i32 = 36; let num2 = num1; }