[英]How can a function require that a type implement a trait without removing the existing trait bound?
I'm trying to have a main_func
that returns a vector of a type T
structs with a SrObject
trait 我正在尝试使
main_func
返回具有SrObject
特征的T
型结构的SrObject
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;
}
it gives: 它给:
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`
I understand that I don't have the Default
trait in fn main_func<T: SrObject>
, but how can I achieve this without removing the SrObject
trait? 我了解我在fn
main_func<T: SrObject>
没有Default
trait,但是如何在不删除SrObject
trait的情况下实现这一SrObject
?
I'd encourage you to go back and reread The Rust Programming Language . 我鼓励您回去重读Rust编程语言 。 This is a free online book that the Rust community has created that covers a broad spectrum of the things you need to know to be a successful Rust programmer.
这是Rust社区创建的免费在线书籍,涵盖了成为一名成功的Rust程序员所需了解的广泛知识。
In this case, the 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
.如果我们需要能够在函数和
summary
方法中使用类型T
上的显示格式,则可以使用特征范围T: Summarizable + Display
。 This meansT
can be any type that implements bothSummarizable
andDisplay
.这意味着
T
可以是实现Summarizable
和Display
任何类型。
For your case: 对于您的情况:
fn main_func<T: SrObject + Default>() -> Vec<T> {
(0..10).map(|_| T::default()).collect()
}
Or 要么
fn main_func<T>() -> Vec<T>
where
T: SrObject + Default,
{
(0..10).map(|_| T::default()).collect()
}
Other changes to make it idiomatic: 使其变得惯用的其他更改:
v
when calling Vec::new
; Vec::new
时不要指定v
的类型; it will be inferred. return
at the end of a function. return
。 Iterator::map
and Iterator::collect
to convert the iterator into a collection, instead of pushing elements by hand. Iterator::map
和Iterator::collect
将迭代器转换为集合,而不是手动推送元素。 See also: 也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.