Seems impossible by specifying the type directly:
fn main() {
let a:&mut str = "".into();
}
Seems impossible by specifying the type directly:
fn main() {
let a:&mut str = "".into();
}
Why would you expect a string literal to be mutable?
You can make a &mut str
by mutably borrowing a String. However, due to the requirement of strings being valid UTF-8, a mutable str is basically useless (you can't push onto its end, so all you can do with it is unsafely manipulating its bytes, which you shouldn't do.)
I'm afraid you must use something like let a: &mut str = String::from("hello").as_mut_str();
or let a: &mut str = &mut String::from("hello");
There are some safe operations you can make on a &mut str
, for example:
For the empty string specifically, you can
let _: &'static mut str = std::str::from_utf8_mut(<_>::default()).unwrap();
(As a special case, an empty &mut []
of any type and lifetime can be conjured, as it points to no memory.)