最初我写了这个:
fn split_line(line: &String) -> Vec<String> {
let mut chars = line.chars();
let mut vec = Vec::new();
let mut s = String::from("");
while let Some(x) = chars.next() {
match x {
'<' => {},
'/' => {
vec.push(s);
break;
}
' ' => {
vec.push(s);
s.clear();
}
_ => s.push(x),
}
}
vec
}
并收到此错误:
use of moved value: 's'
所以我将vec.push(s)
更改为vec.push(&s)
,删除了原始错误,但将vec从std::vec::Vec<std::string::String>
更改为std::vec::Vec<&std::string::String>
。
为什么呢?如何在不改变vec的情况下借用s?
答案 0 :(得分:2)
当您执行ans = ans * base
时,vec.push(s)
会移动到向量中,这意味着s
不再包含有意义的值。您只需将s
重置为新的s
。
String