簡體   English   中英

在實現指定特征的結構中添加成員

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM