的实现std::mem::drop
记录如下:
pub fn drop<T>(_x: T) { }
因此,我希望该封盖|_| ()
(俗称马桶盖)drop
在两个方向上都可以按1:1的比例替换。但是,下面的代码显示drop
与该函数的参数上绑定的较高特级特征不兼容,而马桶盖则兼容。
fn foo<F, T>(f: F, x: T)
where
for<'a> F: FnOnce(&'a T),
{
dbg!(f(&x));
}
fn main() {
foo(|_| (), "toilet closure"); // this compiles
foo(drop, "drop"); // this does not!
}
编译器的错误信息:
error[E0631]: type mismatch in function arguments
--> src/main.rs:10:5
|
1 | fn foo<F, T>(f: F, x: T)
| ---
2 | where
3 | for<'a> F: FnOnce(&'a T),
| ------------- required by this bound in `foo`
...
10 | foo(drop, "drop"); // this does not!
| ^^^
| |
| expected signature of `for<'a> fn(&'a _) -> _`
| found signature of `fn(_) -> _`
error[E0271]: type mismatch resolving `for<'a> <fn(_) {std::mem::drop::<_>} as std::ops::FnOnce<(&'a _,)>>::Output == ()`
--> src/main.rs:10:5
|
1 | fn foo<F, T>(f: F, x: T)
| ---
2 | where
3 | for<'a> F: FnOnce(&'a T),
| ------------- required by this bound in `foo`
...
10 | foo(drop, "drop"); // this does not!
| ^^^ expected bound lifetime parameter 'a, found concrete lifetime
考虑到drop
对于任何大小都应该是通用的,T
“更通用”的签名fn(_) -> _
与不兼容听起来是不合理的for<'a> fn (&'a _) -> _
。为什么编译器不接受drop
此处的签名?当将马桶盖放回原处时,它为何与众不同?