Programming Languages Rust Subjective
Oct 04, 2025

Explain the difference between String and &str in Rust.

Detailed Explanation
String vs &str differences: • String: owned, heap-allocated, growable • &str: borrowed, string slice, immutable • String can be modified, &str cannot • &str is more efficient for read-only operations • String literals are &str type Example: let owned: String = String::from("hello"); let borrowed: &str = "world"; let slice: &str = &owned[0..2]; fn takes_str(s: &str) { } fn takes_string(s: String) { }
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback