我对生锈很陌生,还在读书
the book
今天我试着写一个建议作为练习的程序(更准确地说是最后一个在
the end of chapter 8.3
). 由于我仍在学习,因此速度很慢,我运行了一个新的
cargo build
对于我添加到
main.rs
use std::io::{self, Write};
use std::collections::{HashMap, HashSet};
enum Command<'a> {
Add {name: &'a str, unit: &'a str},
List {unit: &'a str},
Exit
}
fn main() {
let mut units: HashMap<&str, HashSet<&str>> = HashMap::new();
loop {
let mut cmd = String::new();
io::stdin().read_line(&mut cmd).unwrap();
let cmd = match parse_command(&cmd) {
Ok(command) => command,
Err(error) => {
println!("Error: {}!", error);
continue;
}
};
match cmd {
Command::Add {name: new_name, unit: new_unit} => {
let mut u = units.entry("unit1").or_insert(HashSet::new());
u.insert(new_name);
},
Command::List {unit: target_unit} => {},
Command::Exit => break
}
}
}
fn parse_command<'a>(line: &'a String) -> Result<Command<'a>, &'a str> {
Ok(Command::Exit)
}
没什么复杂的,从那以后我甚至还没有在我的
parse_command
Result::Ok(Command::Exit)
,但当我尝试编译上述代码时,我得到了以下错误:
error[E0597]: `cmd` does not live long enough
--> src/main.rs:34:2
|
17 | let cmd = match parse_command(&cmd) {
| --- borrow occurs here
...
34 | } // end of loop
| ^ `cmd` dropped here while still borrowed
35 | } // end of main
| - borrowed value needs to live until here
cmd
在
loop
,没关系,但是
main
?
任何与
命令
环
为了找出问题所在,我删除了
match
手臂
Command::Add {...}
match cmd {
Command::Add {name: new_name, unit: new_unit} => {},
Command::List {unit: target_unit} => {},
Command::Exit => break
}
令我惊讶的是,代码编译时没有出错(尽管我需要这些行,所以这只是一个愚蠢的测试)。
我以为这两条线
与我的
命令