我正在尝试解决一个涉及比较两组的在线挑战。我按照这个答案将我的Vec<i32>输出转换为HashSet
use std::collections::HashSet;
use std::iter::FromIterator;
struct Solution {}
impl Solution {
pub fn solve(nums: Vec<i32>, k: i32) -> Vec<i32> {
// todo, return dummy for now
return vec![1, 2];
}
}
fn main() {
assert_eq!(
HashSet::from_iter(Solution::solve(vec![1, 2, 3], 2)),
HashSet::from_iter(vec![1i32, 2i32])
)
}
Run Code Online (Sandbox Code Playgroud)
由于我还不明白的原因,编译失败:
error[E0282]: type annotations needed
--> src/main.rs:15:9
|
15 | HashSet::from_iter(Solution::solve(vec![1, 2, 3], 2)),
| ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `S` declared on the struct `HashSet`
Run Code Online (Sandbox Code Playgroud)
它适用于 HashSet::from_iter(vec![1i32, 2i32])
我尝试添加类型注释 …
rust ×1