当我试图使用另一个
HashSet<String>
扩展 HashSet<String>
时:use std::collections::HashSet;
let mut a = HashSet::new();
a.insert("foo".to_owned());
let mut b = HashSet::new();
b.insert("bar".to_owned());
let c = a.extend(&b);
我理解为:
error[E0271]: type mismatch resolving `<&HashSet<String> as IntoIterator>::Item == String`
--> src/main.rs:7:11
|
7 | let c = a.extend(&b);
| ^^^^^^ expected reference, found struct `String`
|
= note: expected reference `&String`
found struct `String`
我该如何做这件事?
a.extend(&b)
,但对于 OP 来说失败了。在另一个答案中它有效是因为另一个答案使用的集合元素是可复制的(Copy
)。(我有点惊讶a.extend(&b)
对于可复制的元素有效,不知道是什么机制实现了它。) - user4815162342