为什么 Rust 可变借用发生在这里?
Why Rust mutable borrow occurs here?
我正在学习 Rust,下面的代码来自在线书籍 The Rust Programming Language。
fn main() {
let mut s = String::from("hello world");
let word = first_word(&s);
s.clear(); // error!
println!("the first word is: {}", word);
}
fn first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
当我 运行 它时,我得到这个:
C:/Users/administrator/.cargo/bin/cargo.exe run --color=always --package rust2 --bin rust2
Compiling rust2 v0.1.0 (C:\my_projects\rust2)
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
--> src\main.rs:6:5
|
4 | let word = first_word(&s);
| -- immutable borrow occurs here
5 |
6 | s.clear(); // error!
| ^^^^^^^^^ mutable borrow occurs here
7 |
8 | println!("the first word is: {}", word);
| ---- immutable borrow later used here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0502`.
error: could not compile `rust2`.
To learn more, run the command again with --verbose.
Process finished with exit code 101
但据我了解,s
只是一个可变的 String
对象。 s.clear()
只是在对象上调用一个方法,这会产生一个 mutable borrow 错误?可变借用类似于 let mut a = &mut s
。 s.clear()
语句是直接使用s
,借用从何而来?
The statement s.clear() is using s directly, where does the borrow come from?
方法的第一个参数始终是 self
,它表示调用该方法的结构实例。如果不想取得所有权,只是读取结构中的数据而不写入数据,则可以选择 &self
。另一方面,如果要更改调用方法的实例,则可以选择 &mut self
。最后但并非最不重要的一点是,self
取得所有权,通常会转化为其他东西。
这里,String::clear
定义为:
pub fn clear(&mut self)
这是一个可变的借用。如果用另一种方式调用clear
方法,你可以清楚地看到原因:
let word = first_word(&s);
String::clear(&mut s);
println!("the first word is: {}", word);
我正在学习 Rust,下面的代码来自在线书籍 The Rust Programming Language。
fn main() {
let mut s = String::from("hello world");
let word = first_word(&s);
s.clear(); // error!
println!("the first word is: {}", word);
}
fn first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
当我 运行 它时,我得到这个:
C:/Users/administrator/.cargo/bin/cargo.exe run --color=always --package rust2 --bin rust2
Compiling rust2 v0.1.0 (C:\my_projects\rust2)
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
--> src\main.rs:6:5
|
4 | let word = first_word(&s);
| -- immutable borrow occurs here
5 |
6 | s.clear(); // error!
| ^^^^^^^^^ mutable borrow occurs here
7 |
8 | println!("the first word is: {}", word);
| ---- immutable borrow later used here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0502`.
error: could not compile `rust2`.
To learn more, run the command again with --verbose.
Process finished with exit code 101
但据我了解,s
只是一个可变的 String
对象。 s.clear()
只是在对象上调用一个方法,这会产生一个 mutable borrow 错误?可变借用类似于 let mut a = &mut s
。 s.clear()
语句是直接使用s
,借用从何而来?
The statement s.clear() is using s directly, where does the borrow come from?
方法的第一个参数始终是 self
,它表示调用该方法的结构实例。如果不想取得所有权,只是读取结构中的数据而不写入数据,则可以选择 &self
。另一方面,如果要更改调用方法的实例,则可以选择 &mut self
。最后但并非最不重要的一点是,self
取得所有权,通常会转化为其他东西。
这里,String::clear
定义为:
pub fn clear(&mut self)
这是一个可变的借用。如果用另一种方式调用clear
方法,你可以清楚地看到原因:
let word = first_word(&s);
String::clear(&mut s);
println!("the first word is: {}", word);