私はクレートを使用して、単語のベクトルを枝打ちさせようとしています。それは簡単であるべき、しかしボローチェッカーは、私のコードを拒否し続けている:2つの変更可能な借用は同じ行で行われますか?
// 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);
}
// …
}
ボローチェッカーは、私はライン*t = stemmer.stem_str(t);
上stemmer
の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)
これは、このライブラリの実装の詳細とは何かを持っている場合、私はわからないんだけど、私は本当にここで立ち往生感じています。私は、入力のベクトルを形づくることはとても難しいとは決して考えなかった。 documentation of stem_str
から
'or_exit(上記のコメントで言うように)' '以来、マクロではないようです! 'がありません(タイプミス?)。また、完全なコンパイラエラーを追加できますか? :) –
@ LukasKalbertodtこれは、標準ライブラリではなく、コードベースで定義されたものです。おそらくコードサンプルを単に 'unwrap()'に変更するべきです。私は完全なエラーを含めました。 –