一个函数如何要求一个类型实现一个特征而不删除现有的特征边界?
How can a function require that a type implement a trait without removing the existing trait bound?
我正在尝试 main_func
returns 具有 SrObject
特征
的类型 T
结构的向量
struct TestA {
value: u8,
}
pub trait SrObject {
fn myfunc(&mut self);
}
impl SrObject for TestA {
fn myfunc(&mut self) {
unimplemented!();
}
}
impl Default for TestA {
fn default() -> TestA {
TestA { value: 3u8 }
}
}
fn main_func<T: SrObject>(t: T) -> Vec<T> {
let mut v = Vec::<T>::new();
for i in 0..10 {
v.push(T::default());
//v[i].myfunc();
}
return v;
}
它给出:
error[E0599]: no function or associated item named `default` found for type `T` in the current scope
--> src/main.rs:22:16
|
22 | v.push(T::default());
| ^^^^^^^^^^ function or associated item not found in `T`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `default`, perhaps you need to implement it:
candidate #1: `std::default::Default`
我知道我在 fn main_func<T: SrObject>
中没有 Default
特征,但是我如何在不删除 SrObject
特征的情况下实现这一点?
我鼓励你回去重读 The Rust Programming Language。这是 Rust 社区创建的一本免费在线书籍,涵盖了成为一名成功的 Rust 程序员所需了解的广泛内容。
在这种情况下,chapter on traits mentions this about trait bounds:
We can specify multiple trait bounds on a generic type by using +
. If we needed to be able to use display formatting on the type T
in a function as well as the summary
method, we can use the trait bounds T: Summarizable + Display
. This means T
can be any type that implements both Summarizable
and Display
.
对于你的情况:
fn main_func<T: SrObject + Default>() -> Vec<T> {
(0..10).map(|_| T::default()).collect()
}
或
fn main_func<T>() -> Vec<T>
where
T: SrObject + Default,
{
(0..10).map(|_| T::default()).collect()
}
使它符合习惯的其他更改:
- 调用
Vec::new
时不指定v
的类型;它将被推断。
- 不要在函数末尾使用显式
return
。
- 使用
Iterator::map
and Iterator::collect
将迭代器转换为集合,而不是手动推送元素。
另请参阅:
我正在尝试 main_func
returns 具有 SrObject
特征
T
结构的向量
struct TestA {
value: u8,
}
pub trait SrObject {
fn myfunc(&mut self);
}
impl SrObject for TestA {
fn myfunc(&mut self) {
unimplemented!();
}
}
impl Default for TestA {
fn default() -> TestA {
TestA { value: 3u8 }
}
}
fn main_func<T: SrObject>(t: T) -> Vec<T> {
let mut v = Vec::<T>::new();
for i in 0..10 {
v.push(T::default());
//v[i].myfunc();
}
return v;
}
它给出:
error[E0599]: no function or associated item named `default` found for type `T` in the current scope
--> src/main.rs:22:16
|
22 | v.push(T::default());
| ^^^^^^^^^^ function or associated item not found in `T`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `default`, perhaps you need to implement it:
candidate #1: `std::default::Default`
我知道我在 fn main_func<T: SrObject>
中没有 Default
特征,但是我如何在不删除 SrObject
特征的情况下实现这一点?
我鼓励你回去重读 The Rust Programming Language。这是 Rust 社区创建的一本免费在线书籍,涵盖了成为一名成功的 Rust 程序员所需了解的广泛内容。
在这种情况下,chapter on traits mentions this about trait bounds:
We can specify multiple trait bounds on a generic type by using
+
. If we needed to be able to use display formatting on the typeT
in a function as well as thesummary
method, we can use the trait boundsT: Summarizable + Display
. This meansT
can be any type that implements bothSummarizable
andDisplay
.
对于你的情况:
fn main_func<T: SrObject + Default>() -> Vec<T> {
(0..10).map(|_| T::default()).collect()
}
或
fn main_func<T>() -> Vec<T>
where
T: SrObject + Default,
{
(0..10).map(|_| T::default()).collect()
}
使它符合习惯的其他更改:
- 调用
Vec::new
时不指定v
的类型;它将被推断。 - 不要在函数末尾使用显式
return
。 - 使用
Iterator::map
andIterator::collect
将迭代器转换为集合,而不是手动推送元素。
另请参阅: