我试图在Rust中使用Snowball stemmer箱子来干掉一个单词向量。它应该是简单的,但借检查一直拒绝我的代码:两个可变借款发生在同一行?
// Read user input
let input = stdin();
let mut stemmer = Stemmer::new("english").unwrap();
for line in input.lock().lines() {
let line = line.unwrap();
let mut query: Vec<_> = line.split_whitespace().collect();
for t in &mut query {
*t = stemmer.stem_str(t);
}
// …
}
借入检查说我有stemmer
上线*t = stemmer.stem_str(t);
2条可变借入并拒绝我的代码。 (80行是for line in input.lock().lines()
末块。)
57 18 error E0499 cannot borrow `stemmer` as mutable more than once at a time (first mutable borrow occurs here) (rust-cargo)
57 18 error E0499 cannot borrow `stemmer` as mutable more than once at a time (second mutable borrow occurs here) (rust-cargo)
80 5 info E0499 first borrow ends here (rust-cargo)
如果我直接调用stem()
方法,我得到一个String
,但我不能只叫as_str()
,并期望获得的&str
分配回*t
,因为借款检查员抱怨“借来的价值不够长”。
57 18 error borrowed value does not live long enough (temporary value created here) (rust-cargo)
57 18 info consider using a `let` binding to increase its lifetime (rust-cargo)
57 42 info temporary value only lives until here (rust-cargo)
80 5 info temporary value needs to live until here (rust-cargo)
我不确定这是否与该库的实现细节有关,但我真的觉得在这里停滞不前。我从来没有想过要扼杀一批投入品会如此困难。
'or_exit()'似乎不是一个宏,因为你在评论上面说的,因为'! '丢失(错字?)。另外:你可以添加完整的编译器错误? :) –
@LukasKalbertodt这是在代码库中定义的东西,而不是标准库。我应该在这里简单地改变代码示例到'unwrap()'。我现在包含完整的错误。 –