Hi everyone,
Im trying to pass a str as a slice of u8 without any success, can someone please point me in the right direction.
Also i cant use String (or any heap allocation, im in a #![no_std] environment) needs to be an &str and the function argument needs to be mutable.
Compiling playground v0.0.1 (/playground)
error[E0596]: cannot borrow `*mssg` as mutable, as it is behind a `&` reference
--> src/main.rs:6:30
|
4 | let mut mssg = "Heyyy!";
| -------- help: consider changing this to be a mutable reference: `&mut str`
5 |
6 | let mut bytes = unsafe { mssg.as_bytes_mut() };
| ^^^^ `mssg` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
Because a string literal (or the byte literal I used) compiles down to a reference into a constant stored in the .rodata section of the binary, so the memory that holds it is immutable. That said, with a &str you could still have copied it to a local variable on the stack like this:
Also converting a non-static &mut str into an &mut [u8] would allow writing non-UTF-8 data to the string, which is UB.
let mut string: String = "a".to_string();
let s: &mut str = &mut *string;
let bytes: &mut [u8] = convert_str_to_bytes(s);
bytes[0] = 0xff; // oops, the string is now the sequence of bytes [0xff], which is not valid UTF-8.