RRust By Example
intermediateOwnership

Mutable Borrow

Use mutable references to modify borrowed data.

Mutable Borrow

Use mutable references to modify borrowed data.

Difficulty

Intermediate

Code

rust
fn push_world(s: &mut String) {
    s.push_str(", world!");
}

fn main() {
    let mut s = String::from("hello");
    push_world(&mut s);
    println!("{}", s);
}

Explanation

Mutable references (&mut) allow modification. Only one mutable reference at a time.

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