principle
https://doc.rust-lang.org/reference/types/closure.html?highlight=fnonce#closure-types
demo
1
fn f<F : FnOnce() -> String> (g: F) {
println!("{}", std::mem::size_of::<F>());
println!("{}", g());
}
fn main() {
// an immutable string slice(&str)
let s = "foo";
f(|| {
s.to_string()
});
}
The output will be:
8
foo
2
fn f<F : FnOnce() -> String> (g: F) {
println!("{}", std::mem::size_of::<F>());
println!("{}", g());
}
fn main() {
// type String
let s = "foo".to_string();
f(|| {
s
});
}
The output will be:
24
foo
标签:closure,String,rust,println,foo,fn,size
From: https://www.cnblogs.com/lddcool/p/18365247