[英]Add a member in a struct that implements a trait specified
假設我在 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
}
我 go 怎么說member
不必是 u32 ,而應該簡單地將u32
實現ReturnsSelf
特征? 我還可以說成員應該實現ReturnsSelf
還是u32
? 或者兩者兼而有之?
您聲明一個具有特征綁定的泛型:
struct Data<T: RetursSelf> {
member: T,
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.