小编Yan*_*unk的帖子

当 Trait 定义仅使用 self 时,为什么 Trait 实现中允许使用 mut self?

我正在traits2.rsruslings 中进行练习,并且对 Rust 的特征语法感到困惑。我有以下工作解决方案(编译并通过测试,我使用 Rust 1.50):

trait AppendBar {
    fn append_bar(self) -> Self;
}

impl AppendBar for Vec<String> {
    fn append_bar(mut self) -> Self {
        self.push("Bar".into());
        self
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,我感到困惑的是,虽然特征定义是fn append_bar(self) -> Self,但我的实现是,它在签名上fn append_bar(mut self) -> Self有一个附加的。mut为什么这是允许的?

traits rust

16
推荐指数
1
解决办法
3586
查看次数

标签 统计

rust ×1

traits ×1