gpt4 book ai didi

rust - 为什么不可变字符串可以调用 String::add(mut self, other: &str)

转载 作者:行者123 更新时间:2023-12-03 11:27:32 24 4
gpt4 key购买 nike

<分区>

标准库字符串.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,我只想知道为什么。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com