Created
July 25, 2017 12:33
-
-
Save anonymous/828b08ae4e7465c2b5a5597e9d4402e0 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
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
fn main() { | |
let s = "def".to_string(); | |
foo(|_| unsafe{as_mut_static(&s)}); | |
} | |
fn foo<F: Fn(&u32) -> &mut String>(f: F) { | |
let x = 4; | |
let s = f(&x); | |
s.push_str("aaa "); | |
s.push_str(&f(&x)); | |
println!("TESTA {}", s); | |
} | |
unsafe fn as_static<T>(t_ref: &T) -> &'static T { | |
&*(t_ref as *const T) | |
} | |
unsafe fn as_mut_static<T>(t_ref: &T) -> &'static mut T { | |
&mut *(t_ref as *const T as *mut T) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment