【发布时间】:2021-01-12 11:07:36
【问题描述】:
在 stdlib 中 字符串.rs:
impl Add<&str> for String {
type Output = String;
#[inline]
fn add(mut self, other: &str) -> String {
self.push_str(other);
self
}
}
let s1 = String::from("tic");
let s2 = String::from("tac");
let s = s1 + &s2;// it works
s1在这里是不可变的,但是 Add::add(mut self, other: &str) 是mut,我只是想知道为什么。
【问题讨论】:
-
AFAIK,
mut self与&mut self不同,您的变量只能在第二种情况下是可变的
标签: rust move-semantics