Programming Languages
Rust
Subjective
Oct 04, 2025
Explain ownership in Rust and how it prevents memory leaks.
Detailed Explanation
Ownership in Rust:
• Each value has a single owner
• When owner goes out of scope, value is dropped
• Prevents double-free and use-after-free errors
• No garbage collector needed
• Move semantics by default
Example:
fn main() {
let s1 = String::from("hello");
let s2 = s1; // s1 moved to s2
// println!("{}", s1); // Error: s1 no longer valid
println!("{}", s2); // OK
}
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts