usestd::thread::sleep;usestd::time::{Duration, Instant};fnmain(){measure_time(able_to_pass);letmut i =0;measure_time(||{ i =able_to_pass_with_return();});// result: 1
println!("i = {}", i);// 带参数的无法工作
// measure_time(able_to_pass_with_parameter);
// 但是你可以通过closure来完成
measure_time(||{able_to_pass_with_parameter("test".to_string());});measure_time(||{println!("works!");sleep(Duration::new(1,0));});}fnable_to_pass(){println!("works! in function");}fnable_to_pass_with_return()->i32{println!("works! in function (return)");return1;}fnable_to_pass_with_parameter(x: String){println!("works! in function with {}", x);}// https://stackoverflow.com/a/25182801/8587335
fnmeasure_time<F:FnOnce()>(func: F){let start =Instant::now();func();let duration = start.elapsed();println!("Time elapsedpsed in is: {:?}", duration);}