【发布时间】:2018-04-21 05:18:39
【问题描述】:
我有:
struct Plumbus<'a> {
grumbo: &'a Grumbo,
}
trait Grumbo {
fn dinglebop<T>(&self, x: &mut T) -> bool { false }
}
但我明白了:
error[E0038]: the trait `Grumbo` cannot be made into an object
--> plumbus.rs:4:5
|
4 | grumbo: &'a Grumbo,
| ^^^^^^^^^^^^^^^^^^ the trait `Grumbo` cannot be made into an object
|
= note: method `dinglebop` has generic type parameters
我想让dinglebop 默认不做任何事情,但取决于Grumbo 和T,如果对特定的Grumbo 实现有意义,可能用x 填充T。
在 C++ 中,这可能通过部分特化来完成。我不确定 Rust 的目标是什么。
- 是否可以在 trait 上使用这样的通用函数?
- 如何在不将
Plumbus专门用于特定T的情况下,实现将dinglebop()用于任意T的目标?
【问题讨论】:
-
阅读this。这可能是重复的吗?