I'm learning Rust and learned, that for making an expandable string or array, the String
and Vec
structs are used. And to modify a String
or Vec
, the corresponding variable needs to be annotated with mut
.
let mut myString = String::from("Hello");
let mut myArray = vec![1, 2, 3];
My question is, why would you use or declare an immutable String
or Vec
like
let myString = String::from("Hello");
let myArray = vec![1, 2, 3];
instead of a true array or str literal like
let myString = "Hello";
let myArray = [1, 2, 3];
What would be the point of that, does it have any benefits? And what may be other use cases for immutable String
's and Vec
's?
Edit:
Either I am completely missing something obvious or my question isn't fully understood. I get why you want to use a mutable String
or Vec
over the str literal or an array, since the latter are immutable. But why would one use an immutable String
or Vec
over the str literal or the array (which are also immutable)?