我对 Option::map()
有点困惑。 documentation表示它接受 FnOnce
。
如果是这样,为什么a
和b
会导致编译错误?
let mut v = 3;
let mut a: &FnOnce(u32) -> u32 = &|x: u32| { v = x; x };
let mut b: &FnMut(u32) -> u32 = &|x: u32| { x };
let mut c: &Fn(u32) -> u32 = &|x: u32| { x };
let o = Option::Some(3);
o.map(a); // trait bound `std::ops::FnOnce(u32) -> u32: std::ops::Fn<(u32,)>` is not satisfied
o.map(b); // trait bound `std::ops::FnMut(u32) -> u32: std::ops::Fn<(u32,)>` is not satisfied
o.map(c); // works
根据this post,它们不应该全部实现FnOnce
,包括a
和b
吗? ?
最佳答案
问题是您不是直接使用 FnOnce
调用 Option::map
,而是使用 &FnOnce
。
但是如果你看一下 implementors for FnOnce
,您会注意到虽然 FnOnce
是为 &Fn
实现的,但它不是为 &FnOnce
或 &FnMut
实现的。
要了解原因,请考虑以下几点:
let a: &FnOnce(u32) -> u32 = &|x: u32| { v = x; x };
let b: &FnOnce(u32) -> u32 = a;
a(42); // would be allowed if `&FnOnce: FnOnce`, moves `a`
// actually gives:
// error[E0507]: cannot move out of borrowed content
a(6); // would not be allowed as `a` was moved
b(7); // would be allowed if `&FnOnce: FnOnce`
// oh no! this calls the function again!
关于rust - Option::map(FnOnce) 似乎不接受 FnOnce ...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47933779/