英文:
Store function returning impl Trait as trait object
问题
根据下面的示例,我正在尝试将函数存储为特征对象。我已经弄清楚了如何对返回具体类型的函数执行此操作,方法是使用 as
将函数项转换为函数指针。然而,我也想存储返回 impl Element
的函数在 Vector 中,就我所了解的而言,这需要在 HasChildren
实现中使用泛型参数,但接下来我不知道该如何或应该将其转换为什么,以便将其用作特征对象。
trait Element {}
trait HasChildren {}
struct Foo {}
impl Element for Foo {}
impl HasChildren for Foo {}
impl HasChildren for fn(String) -> Foo {}
impl<E: Element> HasChildren for fn(i32) -> E {}
fn get_concrete_element(val: String) -> Foo {
Foo {}
}
fn get_impl_element(val: i32) -> impl Element {
Foo {}
}
fn main() {
// 在 Vec 中存储各种特征对象
let mut data: Vec<Box<dyn HasChildren>> = Vec::new();
data.push(Box::new(Foo {})); // 工作正常
data.push(Box::new(get_concrete_element as fn(String) -> Foo)); // 工作正常
data.push(Box::new(get_impl_element as fn(i32) -> impl Element)); // 不工作
}
英文:
As per the below example, I am trying to store functions as trait objects. I have worked out how to do this for functions which return a concrete type, by using as
to cast from a function item to a function pointer. However, I would also like to store functions that return impl Element
in the Vector, which as far as I understand requires using a generic parameter in the HasChildren
implementation, but then I don't know how or what I should cast it to so it can be used as a trait object.
trait Element {}
trait HasChildren {}
struct Foo {}
impl Element for Foo {}
impl HasChildren for Foo {}
impl HasChildren for fn(String) -> Foo {}
impl<E: Element> HasChildren for fn(i32) -> E {}
fn get_concrete_element(val: String) -> Foo {
Foo {}
}
fn get_impl_element(val: i32) -> impl Element {
Foo {}
}
fn main() {
// Store various trait objects in Vec
let mut data: Vec<Box<dyn HasChildren>> = Vec::new();
data.push(Box::new(Foo {})); // works
data.push(Box::new(get_concrete_element as fn(String) -> Foo)); // works
data.push(Box::new(get_impl_element as fn(i32) -> impl Element)); // doesn't work
}
答案1
得分: 2
你不能指定返回类型的名称,但你可以让编译器推断它:
```rust
data.push(Box::new(get_impl_element as fn(i32) -> _));
英文:
You cannot name the return type, but you can let the compiler infer it:
data.push(Box::new(get_impl_element as fn(i32) -> _));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论