UniCase
没有实现Borrow
,这意味着如果不分配String
,就不能用UniCase
键索引map。建议的解决方法是使用HashMap<UniCase<Cow<str>>, _>
。不幸的是,它并不总是编译;
use std::borrow::Cow;
use std::collections::HashMap;
use std::io::Result;
use unicase::UniCase;
fn rm_reader_keys<R: BufRead>(
reader: R, dict: &mut HashMap<UniCase<Cow<str>>, String>
) -> Result<()> {
for ln in reader.lines() {
let line: String = ln?;
if let Some((key, _)) = line.rsplit_once(' ') {
dict.remove(&UniCase::unicode(Cow::Borrowed(key)));
}
}
Ok(())
}
产生
error[E0597]: `line` does not live long enough
--> src/main.rs:44:33
|
40 | reader: R, dict: &mut HashMap<UniCase<Cow<str>>, String>
| ---- has type `&mut HashMap<UniCase<Cow<'1, str>>, std::string::String>`
...
43 | let line: String = ln?;
| ---- binding `line` declared here
44 | if let Some((key, _)) = line.rsplit_once(' ') {
| ^^^^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
45 | dict.remove(&UniCase::unicode(Cow::Borrowed(key)));
| -------------------------------------------------- argument requires that `line` is borrowed for `'1`
46 | }
47 | }
| - `line` dropped here while still borrowed
有办法解决吗?
1条答案
按热度按时间i1icjdpr1#
我不相信这是可能的问题是类型必须完全相同,所以我们需要与函数的参数相同的生存期。但它是通用的,我们没有办法让字符串存活那么长时间。
但是,您可以直接使用
hashbrown
。它具有Equivalent
特征,比Borrow
更通用: