在模式匹配中借用
Borrowing within pattern matching
我正在编写一个 Rust 程序来收集每个输入行的第一个单词,这有点类似于 Unix 实用程序 cut
。
use std::io;
fn main() {
let mut words = Vec::new();
let mut input = String::new();
loop {
match io::stdin().read_line(&mut input) {
std::result::Result::Ok(_) => {
let words_line: Vec<&str> = input.split_whitespace().collect();
match words_line.get(0) {
Some(&word) => {
words.push(word.clone());
},
_ => continue,
}
}
std::result::Result::Err(_) => break
}
}
println!("{:?}", words);
}
这给了我
$ cargo run
Compiling foo v0.1.0 (/home/ubuntu/projects/foo)
error[E0502]: cannot borrow `input` as mutable because it is also borrowed as immutable
--> src/main.rs:10:37
|
10 | match io::stdin().read_line(&mut input) {
| ^^^^^^^^^^ mutable borrow occurs here
11 | std::result::Result::Ok(_) => {
12 | let words_line: Vec<&str> = input.split_whitespace().collect();
| ----- immutable borrow occurs here
...
24 | println!("{:?}", words);
| ----- 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 `foo`
To learn more, run the command again with --verbose.
我已经阅读了,但仍然感到困惑:可变借用发生在第 10 行,而不可变借用发生在第 12 行,那么“已经作为不可变借用的变量被借用为可变”怎么可能发生?至少错误应该是“已经借用为 mutable(第 10 行)的变量被借用为 immutable(第 12 行)”。
我想你想让 words
变成 Vec<String>
(截至目前,Rust 试图推断 Vec<&str>
这会带来生命周期问题,因为元素会引用 input
在下一次 loop
迭代时改变。
use std::io;
fn main() {
let mut words : Vec<String> = Vec::new();
let mut input = String::new();
loop {
match io::stdin().read_line(&mut input) {
std::result::Result::Ok(_) => {
let words_line: Vec<&str> = input.split_whitespace().collect();
match words_line.get(0) {
Some(&word) => {
words.push(word.to_string());
},
_ => continue,
}
}
std::result::Result::Err(_) => break
}
}
println!("{:?}", words);
}
作为旁注,collect
进入 words_line
似乎是不必要的:您可以简单地调用 next
(而不是 collect
)来查明是否有第一个元素。
呼叫 io::stdin().read_line(&mut input)
return 是 io::Result<usize>
。由于 return 值不包含任何与您传入的引用相关联的生命周期,因此它不会创建比该调用表达式持续时间更长的借用,因此它不应与稍后发生的不可变借用冲突。事实上,如果你删除 loop
.
代码编译
原因是 word.clone()
实际上没有做任何事情——它创建了一个 &str
的副本,它仍然是一个 &str
。由于它存储在 words
中,input
字符串在循环迭代中被借用。如果您将 word.clone()
替换为 word.to_owned()
.
,您的代码将编译
清理代码很容易避免这个问题。这是一个示例实现,它还修复了 input
从所有行累积数据的错误,我认为你不打算这样做:
while io::stdin().read_line(&mut input).is_ok() {
if let Some(word) = input.split_whitespace().next() {
words.push(word.to_owned());
}
input.clear();
}
另一种选择:
use std::io;
use std::io::BufRead;
fn main() {
let mut words = vec![];
for result in io::stdin().lock().lines() {
match result {
Ok(line) => words.extend(line.split_whitespace().next().map(ToOwned::to_owned)),
Err(_) => break,
}
}
println!("{:?}", words);
}
我正在编写一个 Rust 程序来收集每个输入行的第一个单词,这有点类似于 Unix 实用程序 cut
。
use std::io;
fn main() {
let mut words = Vec::new();
let mut input = String::new();
loop {
match io::stdin().read_line(&mut input) {
std::result::Result::Ok(_) => {
let words_line: Vec<&str> = input.split_whitespace().collect();
match words_line.get(0) {
Some(&word) => {
words.push(word.clone());
},
_ => continue,
}
}
std::result::Result::Err(_) => break
}
}
println!("{:?}", words);
}
这给了我
$ cargo run
Compiling foo v0.1.0 (/home/ubuntu/projects/foo)
error[E0502]: cannot borrow `input` as mutable because it is also borrowed as immutable
--> src/main.rs:10:37
|
10 | match io::stdin().read_line(&mut input) {
| ^^^^^^^^^^ mutable borrow occurs here
11 | std::result::Result::Ok(_) => {
12 | let words_line: Vec<&str> = input.split_whitespace().collect();
| ----- immutable borrow occurs here
...
24 | println!("{:?}", words);
| ----- 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 `foo`
To learn more, run the command again with --verbose.
我已经阅读了
我想你想让 words
变成 Vec<String>
(截至目前,Rust 试图推断 Vec<&str>
这会带来生命周期问题,因为元素会引用 input
在下一次 loop
迭代时改变。
use std::io;
fn main() {
let mut words : Vec<String> = Vec::new();
let mut input = String::new();
loop {
match io::stdin().read_line(&mut input) {
std::result::Result::Ok(_) => {
let words_line: Vec<&str> = input.split_whitespace().collect();
match words_line.get(0) {
Some(&word) => {
words.push(word.to_string());
},
_ => continue,
}
}
std::result::Result::Err(_) => break
}
}
println!("{:?}", words);
}
作为旁注,collect
进入 words_line
似乎是不必要的:您可以简单地调用 next
(而不是 collect
)来查明是否有第一个元素。
呼叫 io::stdin().read_line(&mut input)
return 是 io::Result<usize>
。由于 return 值不包含任何与您传入的引用相关联的生命周期,因此它不会创建比该调用表达式持续时间更长的借用,因此它不应与稍后发生的不可变借用冲突。事实上,如果你删除 loop
.
原因是 word.clone()
实际上没有做任何事情——它创建了一个 &str
的副本,它仍然是一个 &str
。由于它存储在 words
中,input
字符串在循环迭代中被借用。如果您将 word.clone()
替换为 word.to_owned()
.
清理代码很容易避免这个问题。这是一个示例实现,它还修复了 input
从所有行累积数据的错误,我认为你不打算这样做:
while io::stdin().read_line(&mut input).is_ok() {
if let Some(word) = input.split_whitespace().next() {
words.push(word.to_owned());
}
input.clear();
}
另一种选择:
use std::io;
use std::io::BufRead;
fn main() {
let mut words = vec![];
for result in io::stdin().lock().lines() {
match result {
Ok(line) => words.extend(line.split_whitespace().next().map(ToOwned::to_owned)),
Err(_) => break,
}
}
println!("{:?}", words);
}