的实现std::mem::drop记录如下:
pub fn drop<T>(_x: T) { }
Run Code Online (Sandbox Code Playgroud)
因此,我希望该封盖|_| ()(俗称马桶封盖)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!
}
Run Code Online (Sandbox Code Playgroud)
编译器的错误信息:
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),
| …Run Code Online (Sandbox Code Playgroud)