Fn是啥,普通的一款 fn test(f: T) where T: Fn() { f(); } let s = String::from("66"); let f = || {println!("{}", s);}; test(f); 这种就可以了
fn test(f: T) where T: Fn() { f(); } let s = String::from("66"); let f = || {println!("{}", s);}; test(f);
如果f里发生了s.push_str(“7”) 那么明显 这个时候发生了MUT! 只需要把Fn改成FnMut
另外一个操作是Once fn test(f: T)where T: FnOnce() { f(); } 如果执行test(f)这里会发生move,把f直接move了,Once的意思就清晰了,你既然已经move进去test里了,那么test结束的时候 f 就不复存在了,所以就只能一次,就Once了
fn test(f: T)where T: FnOnce() { f(); }
京公网安备 11010502049817号