I'm trying to have a main_func
that returns a vector of a type T
structs with a SrObject
trait
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?
I'd encourage you to go back and reread The Rust Programming Language . 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.
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
. This meansT
can be any type that implements bothSummarizable
andDisplay
.
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
; it will be inferred. return
at the end of a function. Iterator::map
and Iterator::collect
to convert the iterator into a collection, instead of pushing elements by hand. See also:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.