To my knowledge, the following code is intended to be legal:
fn main() { let mut buf = String::with_capacity(11); buf.push_str("hello"); let hello: &str = unsafe { &*(buf.as_str() as *const _) }; // laundering the lifetime -- we take care that `buf` does not reallocate, so that's okay. buf.push_str(" world"); println!("{}", hello); }
However, Miri currently flags this as UB.
I believe this is #60847, but for String
. Discovered while writing this post.
cc @RalfJung
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4