RRust By Example
beginnerOwnership

Ownership Basics

Understand Rust ownership model with move semantics.

Ownership Basics

Understand Rust ownership model with move semantics.

Difficulty

Beginner

Code

rust
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.

Common Compiler Errors in This Topic

Use these error pages as a debugging companion while practicing this example category.

More Ownership Examples