我正在编写一个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.我读过Cannot borrow as mutable because it is also borrowed as immutable,但仍然感到困惑:可变的借入发生在第10行,而不变的借入发生在第12行,那么“已经作为不可变借入的变量作为可变的借入”怎么会发生呢?至少错误应该是“已经借用为可变的变量(在第10行)被借用为不可变的变量(在第12行)”。
发布于 2021-05-25 11:23:52
调用io::stdin().read_line(&mut input)返回一个io::Result<usize>。由于返回值不包含与您传入的引用链接的任何生存期,因此它不会创建比该调用表达式持续时间更长的借入,因此它不应该与以后发生的不可变的借入冲突。实际上,如果删除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);
}发布于 2021-05-25 10:57:10
我认为您希望使words成为一个Vec<String> (到目前为止,Rust试图推断出一个给出终身问题的Vec<&str>,因为元素将引用在下一个loop-iteration上更改的input )。
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)来确定是否存在第一个元素。
https://stackoverflow.com/questions/67685170
复制相似问题