28
loading...
This website collects cookies to deliver better user experience
💡 Ownership IS the only big concept in Rust. It is not unique, and C++ has it too. Understand this well.
fn main() {
let alice = String::from("Teddy");
{
/// Bob suddenly shows up and snatches it from Alice.
let bob = alice;
/// "Bob took it!" Alice cried.
println!("Alice, where is the {}", alice);
let tim = bob;
/// "Tim stole it!" Bob lashed out.
println!("Bob, did you take the {} from Alice?", alice);
/// Tim, an alien under a kid skin, eats the bear.
/// Then, Tim and Bob vanishes into ether.
}
/// "Who's Bob?" Alice said, Teddy no longer in her arm.
/// Cue the X-file music...
println!("Alice, did Bob return the {}", alice);
}
🍼 Try-sies
Instead of a String::from("Teddy")
, try a string literal "Teddy" or an integer and keep the code. What happens? Use this playground.
/// mut means I can change what I have later.
let mut alice = Toy("Teddy");
let bob = alice;
let mut tim = bob;
tim = daghan_does_something(tim);
/// "No!" Alice yelled.
println("Did you get the {:?} back, Alice?", alice);
alice = tim;
/// Print just fine!
println("Now, did you get the {:?} back?", alice);