rust - 如何将 HashSet<&String> 转换为 HashSet?
问题描述
当我执行这部分代码时,出现错误。我知道我必须转换价值,但如何?
编码:
let cleaned_vec:HashSet<_> = new_file_vec.difference(&new_file_vec2).collect();
new_file_vec=cleaned_vec.clone();
错误:
| new_file_vec=cleaned_vec.clone();
| ^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found `&std::string::String`
|
= note: expected struct `std::collections::HashSet<std::string::String>`
found struct `std::collections::HashSet<&std::string::String>`
解决方案
clone
只是克隆里面的引用。你实际上是在转换数据,所以你应该得到一个迭代器,映射克隆,然后收集回一个HashSet
:
let owned_set: HashSet<String> = borrowed_set.iter().map(|&x| x.clone()).collect();
推荐阅读
- methods - 如何简化 CustomKeywords 调用?
- reactjs - NPM throwing 丢失但存在 - 只有一台机器有问题
- javascript - 如何在 Redux 中创建固定宽度的覆盖数组?
- python - 如何做这个清单
- javascript - 滚动Javascript
- c# - 使用 MySql.Data.MySqlClient;不管用; 发布错误
- hibernate - Hibernate 为事务锁定整个表,甚至不让读取该表的数据
- node.js - 错误:无法使用 Google Cloud Function 和 Express 处理请求
- mysql - 使用mysql的时区工作日
- reactjs - 如何在同一样式表 JSS 中引用本地规则