Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::sync::mpsc::channel;
- use std::time::Duration;
- use std::thread;
- fn init_and_spawn<'a, F, G> (f : F) where
- F: 'a + Send + FnOnce() -> G,
- G: 'static + FnOnce(),
- {
- let (send, recv) = channel();
- let boxed = Box::new(f);
- let raw = Box::into_raw(boxed) as usize;
- thread::spawn (move || {
- let boxed = unsafe { Box::from_raw(raw as *mut F) };
- let f = *boxed;
- let g = f();
- let _ = send.send(());
- g()
- });
- let _ = recv.recv();
- }
- fn main() {
- let ref hello = String::from("hello");
- init_and_spawn(|| {
- let clone = hello.clone();
- move || {
- println!("{}", clone);
- }
- });
- thread::sleep(Duration::from_millis(1000));
- }
Add Comment
Please, Sign In to add comment