Skip to content

Instantly share code, notes, and snippets.

Created July 25, 2017 12:33
Show Gist options
  • Save anonymous/828b08ae4e7465c2b5a5597e9d4402e0 to your computer and use it in GitHub Desktop.
Save anonymous/828b08ae4e7465c2b5a5597e9d4402e0 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
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