相关疑难解决方法(0)

为什么`std :: mem :: drop`与排名较高的特征范围中的闭包| _ |()不完全相同?

的实现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)

closures type-inference traits higher-kinded-types rust

9
推荐指数
2
解决办法
160
查看次数