In this code (also on playground), I get no errors during compile:
#![allow(dead_code)]
static FORMAT_STRING: &[u8; 3] = b"%s\0";
static mut FORMAT_STRING_PTR: *const u8 = FORMAT_STRING.as_ptr();
fn main() {
println!("Hello, world!");
}
except if I remove the mut
, then I get the error:
Compiling playground v0.0.1 (/playground)
error[E0277]: `*const u8` cannot be shared between threads safely
--> src/main.rs:4:28
|
4 | static FORMAT_STRING_PTR: *const u8 = FORMAT_STRING.as_ptr();
| ^^^^^^^^^ `*const u8` cannot be shared between threads safely
|
= help: the trait `Sync` is not implemented for `*const u8`
= note: shared static variables must have a type that implements `Sync`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Why is it safe with mut
but isn't without it? It doesn't seem to make sense. It seems like with mut
it should be less safe.
As a side note: I don't want those 2 vars to be mut
, I want them static
, pre-main
() initialized(hopefully not on heap, but ok either way), so I can use them as readonly. (though presumably unsafe code could mutate that string (well, the 3 bytes) if it has pointer to it? and I wouldn't know)
Thanks!
$ rustc -vV
rustc 1.76.0-nightly (07dca489a 2024-02-04) (gentoo)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.6
$ cargo -vV
cargo 1.76.0-nightly
release: 1.76.0-nightly
host: x86_64-unknown-linux-gnu
libgit2: 1.7.1 (sys:0.18.1 vendored)
libcurl: 8.7.1 (sys:0.4.70+curl-8.5.0 system ssl:rustls-ffi/0.12.2/rustls/0.22)
os: Gentoo Linux 2.15 [64-bit]