【发布时间】:2021-01-28 15:45:35
【问题描述】:
我在阅读 rust book 时遇到了这个问题:
fn main() {
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2; // note s1 has been moved here and can no longer be used
}
事实证明'+ 运算符使用add 方法,其签名看起来像这样':
fn add(self, s: &str) -> String { // ... }
我了解s1 被“使用”并且以后不再可用的原因。但是为什么生锈会这样做呢?我的意思是,为什么add 方法不能引用self (&self) 作为第一个参数,以便s1 字符串可供以后使用?
【问题讨论】:
标签: rust