如何在Rust中创建盒装封口矢量?

8 boxing closures rust

以前有一个问题是关于创建一个函数数组,其中函数从一个范围返回整数.最终的解决方案是做一个地图/收集到一个Vec<_>.

我有一个类似但不同的情况,我有相同的签名但不同的实现闭包.我试过这个:

let xs: Vec<_> = vec![
    move |(x, y)| (y, x),
    move |(x, y)| (1 - y, 1 - x),
];
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
4 |         move |(x, y)| (1 - y, 1 - x),
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected closure, found a different closure
  |
  = note: expected type `[closure@src/main.rs:3:9: 3:29]`
             found type `[closure@src/main.rs:4:9: 4:37]`
  = note: no two closures, even if identical, have the same type
  = help: consider boxing your closure and/or using it as a trait object
Run Code Online (Sandbox Code Playgroud)

我试过拳击:

let xs: Vec<_> = vec![
    Box::new(move |x: u8, y: u8| (y, x)),
    Box::new(move |x: u8, y: u8| (1 - y, 1 - x)),
];
Run Code Online (Sandbox Code Playgroud)

我得到了同样的错误:

error[E0308]: mismatched types
 --> src/main.rs:4:18
  |
4 |         Box::new(move |x: u8, y: u8| (1 - y, 1 - x)),
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected closure, found a different closure
  |
  = note: expected type `[closure@src/main.rs:3:18: 3:44]`
             found type `[closure@src/main.rs:4:18: 4:52]`
  = note: no two closures, even if identical, have the same type
  = help: consider boxing your closure and/or using it as a trait object
Run Code Online (Sandbox Code Playgroud)

盒子闭合的正确方法是什么,以便它们可以放入矢量(或数组)?

She*_*ter 16

问题是类型推断在你想要它之前已经开始了.从概念上讲,它是这样的:

let mut xs: Vec<_> = Vec::new();
xs.push(Type1);
xs.push(Type2);
Run Code Online (Sandbox Code Playgroud)

当看到第一个值时,Vec推断出该元素的类型属于该类型.然后第二个元素导致不匹配.

即使你Box的价值观,你也有同样的问题:

let mut xs: Vec<_> = Vec::new();
xs.push(Box::new(Type1));
xs.push(Box::new(Type2));
Run Code Online (Sandbox Code Playgroud)

从另一个角度来看,你从未真正创建过一个特质对象.你有一个Box<ConcreteType>,而不是一个Box<dyn Trait>.

解决方案是将盒装混凝土类型转换为盒装特征对象:

let mut xs: Vec<_> = Vec::new();
xs.push(Box::new(Type1) as Box<dyn Trait>);
xs.push(Box::new(Type2) as Box<dyn Trait>);
Run Code Online (Sandbox Code Playgroud)

第二 push可以有类型自动强制,所以你可以选择离开as有点过该行的.

回滚到原来的问题:

let xs: Vec<_> = vec![
    Box::new(move |(x, y)| (y, x)) as Box<dyn Fn((i32, i32)) -> (i32, i32)>,
    Box::new(move |(x, y)| (1 - y, 1 - x)),
];
Run Code Online (Sandbox Code Playgroud)

或者你可以通过在变量上指定类型来避免推理,我的首选样式是:

let xs: Vec<Box<dyn Fn((i32, i32)) -> (i32, i32)>> = vec![
    Box::new(move |(x, y)| (y, x)),
    Box::new(move |(x, y)| (1 - y, 1 - x)),
];
Run Code Online (Sandbox Code Playgroud)


kaz*_*ase 5

您应该将错误消息中的建议读为“考虑装箱您的闭包并将其用作特征对象,或仅将其用作特征对象”

使用特征对象引用而不装箱它们在这里不起作用,因为没有任何东西拥有闭包。向量中的引用将比闭包更长久:

// This fails
let xs: Vec<&Fn((i32, i32)) -> (i32, i32)> = vec![ 
    &move |(x, y)| (y, x), 
    &move |(x, y)| (1 - y, 1 - x),
];
Run Code Online (Sandbox Code Playgroud)

向量需要拥有闭包的所有权,这就是装箱特征对象发挥作用的地方:

let xs: Vec<Box<Fn((i32, i32)) -> (i32, i32)>> = vec![ 
    Box::new(move |(x, y)| (y, x)), 
    Box::new(move |(x, y)| (1 - y, 1 - x)),
];
Run Code Online (Sandbox Code Playgroud)

这明确告诉编译器该向量可以包含具有相同接口的任何闭包的框。