Ownership Basics
Understand Rust ownership model with move semantics.
Ownership Basics
Understand Rust ownership model with move semantics.
Difficulty
Beginner
Code
fn main() {
let s1 = String::from("hello");
let s2 = s1; // s1 is moved to s2
// println!("{}", s1); // error: value borrowed after move
println!("{}", s2);
}Explanation
Each value has one owner. Assignment moves ownership. Copy types are copied instead of moved.
Key Concepts
- Read the code carefully and understand the data flow
- Try modifying the example to see how it changes behavior
- Run this code in the Rust Playground
Related Topics
Browse more examples in the ownership category to build a complete understanding of this topic.