我尝试运行FnBox
示例来自官方文档 https://doc.rust-lang.org/1.27.0/std/boxed/trait.FnBox.html但它会抛出一个错误:
error[E0432]: unresolved import `std::boxed::FnBox`
--> src/main.rs:4:5
|
4 | use std::boxed::FnBox;
| ^^^^^^^^^^^^^^^^^ no `FnBox` in `boxed`
使用 Rust 很容易出现此错误操场 https://play.rust-lang.org/?version=nightly,我在本地也遇到了同样的错误。
实际上我在本地找到了一些来自 std 的声明:
#[rustc_paren_sugar]
#[unstable(feature = "fnbox",
reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
pub trait FnBox<A>: FnOnce<A> {
/// Performs the call operation.
fn call_box(self: Box<Self>, args: A) -> Self::Output;
}
#[unstable(feature = "fnbox",
reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
impl<A, F> FnBox<A> for F
where F: FnOnce<A>
{
fn call_box(self: Box<F>, args: A) -> F::Output {
self.call_once(args)
}
}
但仍然有一个错误。
FnBox
从未稳定下来。当你想要一个Box<dyn FnOnce()>
但这个类型没有用来实现FnOnce
itself.
Because Box<dyn FnOnce()>实施FnOnce自 Rust 1.35 起 https://github.com/rust-lang/rust/blob/master/RELEASES.md#language-2, FnBox
已在 Rust 1.37 中删除。
的所有用法Box<FnBox<_>>
可以替换为Box<FnOnce<_>>
,例如旧文档示例:
use std::collections::HashMap;
fn make_map() -> HashMap<i32, Box<dyn FnOnce() -> i32>> {
let mut map: HashMap<i32, Box<dyn FnOnce() -> i32>> = HashMap::new();
map.insert(1, Box::new(|| 22));
map.insert(2, Box::new(|| 44));
map
}
fn main() {
let mut map = make_map();
for i in &[1, 2] {
let f = map.remove(&i).unwrap();
assert_eq!(f(), i * 22);
}
}
()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)