ownership Flashcards
understand the principals of ownership
The exact lifetime of each value is controlled by the programmer. Yes or No?
Yes.
Will the compiler check the lifetime of values?
yes.
Does the Rust ownership mechanism prevent dangling pointers?
yes.
Does rust provide a nil value?
No.
In rust, every value has exactly __ owner
one.
when the owner is freed, the value is __
dropped.
Does Rust use a Garbage Collector?
No
T or F. Each value in rust has a variable that is its owner.
True. Except rc
What are the three ownership rules?
- Each value has exactly one owner
- Owners can change, but there can only be one at any instant.
- When the owner goes out of scope, the value is dropped.
What is the restriction for mutable references?
There can be only one mutable reference. There cannot be a mix of mutable and unmutable
What must references always be?
Valid. The must always reference something.
What is the syntax for making a string slice?
let s = “test”.to_string(); let slice = s[..3]; //first three bytes //0,1,2 chars were copied. Not 3
What is the Rust concept of Move
Moving ownership. Assignment will move ownership.
let x = 3; let y = 3; println!("{}", x) // will this work? why ? let x = "blaster".to_string() y = x println!("{}", x) // will this work? why ?
printing the integer will work because integers implement Copy trait. This means they aren’t moved by assignment, but copied on the stack. However, std::string types aren’t copied, they are moved. The second print won’t compile.