Created
May 12, 2019 11:52
-
-
Save nanpuyue/104847a622f5be4dcc4c5f7e456c024f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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