鉴于Vec<isize>这样:
let input = "12
122
100
4444
34
3
66
78
9";
let contents = input.lines()
.map(|l| l.parse().unwrap())
.collect();
Run Code Online (Sandbox Code Playgroud)
我想确定是否contentscontains 2 isizes 加起来为300,我认为该函数应该是这样的。
fn check_for_factors(contents: Vec<isize>, target: isize) -> bool {
contents
.into_iter()
.any(|&x| x + contents[0..n] == target) // or however to loop here
}
check_for_factors(contents, 300);
Run Code Online (Sandbox Code Playgroud)
问题是,我不确定如何contents在any块中循环。这个解决方案可行吗?如何在contents最后一行内循环?
使用combinations适配器可以让生活更轻松并增加代码的可读性。
fn check_for_factors(contents: &[isize], target: isize) -> bool {
contents.iter().combinations(2).any(|x| x[0] + x[1] == target)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1495 次 |
| 最近记录: |