我正在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为什么这是允许的?