Skip to content

Instantly share code, notes, and snippets.

@nanpuyue
Created May 12, 2019 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanpuyue/104847a622f5be4dcc4c5f7e456c024f to your computer and use it in GitHub Desktop.
Save nanpuyue/104847a622f5be4dcc4c5f7e456c024f to your computer and use it in GitHub Desktop.
#![allow(unused_imports)]
use std::mem::{forget, transmute};
use std::slice::from_raw_parts;
use std::str::from_utf8_unchecked;
fn assert_static<T: 'static>(_: T) {}
fn main() {
let str = {
let string = String::from("hello, world!");
let str = unsafe { from_utf8_unchecked(from_raw_parts(string.as_ptr(), string.len())) };
forget(string);
// let str = unsafe { transmute::<_, (&'static str, usize)>(string).0 };
assert_static(str);
str
};
let _string = String::from("!dlrow, olleh");
println!("{}", str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment