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
This example demonstrates how to use mutable borrow in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.
Key Concepts
- Rust's strong type system catches errors at compile time
- Ownership and borrowing rules ensure memory safety
- Pattern matching makes code expressive and exhaustive
Related Topics
Browse more examples in the ownership category to build a complete understanding of this topic.