Let's say I have this struct in Rust:
// Trait that allows objects that
// implement it to return themselves
// to the caller.
trait ReturnsSelf {
fn get_self(self) -> Self {
self
}
}
struct Data {
member: u32
}
How would I go to say that member
doesn't have to be a u32
, but should simply implement ReturnsSelf
as a trait? And can I also say that member should either implement ReturnsSelf
or be a u32
? Or even both?
You declare a generic with a trait bound:
struct Data<T: RetursSelf> {
member: T,
}
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.