[Rust]Summe eines Vec mit Wahrheitswerten
ich verzweifle mal wieder und hoffe das ihr mir wieder helfen könnt. Ich habe einen `Vec` - Objekt mit Wahrheitswerten und will eigentlich nur zählen wie oft `true` darin steckt.
In Python würde ich
Code: Alles auswählen
sum([True, False, True])
Wenn ich in Rust einen `Vec<i32>` habe, dann kann ich auch schreiben:
Code: Alles auswählen
name_des_objekts.iter().sum();
Hier mal der vollständige Code:
Code: Alles auswählen
use std::{
fs::File,
io::{prelude::*, BufReader},
path::Path
};
extern crate itertools;
use itertools::Itertools;
fn is_safe(report: Vec<i32>) -> bool {
let mut safe: Vec<bool> = Vec::new();
for (x, y) in report.into_iter().tuple_windows() {
if 1 <= x - y && x -y <= 3 {
safe.push(true);
} else if 1 <= y - x && y - x <= 3 {
safe.push(true);
} else {safe.push(false)}
};
safe.iter().all(|&x| x == true)
}
fn get_safe_reports(reports: Vec<Vec<i32>>) -> i32 {
let safe_reports: Vec<_>= reports.into_iter().map(|report| is_safe(report)).collect();
safe_reports.iter().sum()
}
fn read_lines(filepath: impl AsRef<Path>) -> Vec<String> {
let file_content = File::open(filepath).expect("File not found");
let buffer = BufReader::new(file_content);
buffer.lines()
.map(|l| l.expect("Can't read"))
.collect()
}
fn format_input(lines: Vec<String>) -> Vec<Vec<i32>> {
let mut reports: Vec<Vec<_>> = Vec::new();
for line in lines {
let line = line.split_whitespace().map(|x| x.parse().unwrap()).collect();
reports.push(line);
};
reports
}
fn main() {
let lines = read_lines("/home/dennis/AoC/2024/src/input02.txt");
let reports = format_input(lines);
println!("{:?}", get_safe_reports(reports));
}
Code: Alles auswählen
[dennis@dennis submarine]$ cargo run
Compiling submarine v0.1.0 (/home/dennis/AoC/submarine)
error[E0277]: a value of type `i32` cannot be made by summing an iterator over elements of type `&bool`
--> src/main.rs:26:25
|
26 | safe_reports.iter().sum()
| ^^^ value of type `i32` cannot be made by summing a `std::iter::Iterator<Item=&bool>`
|
= help: the trait `Sum<&bool>` is not implemented for `i32`
= help: the following other types implement trait `Sum<A>`:
`i32` implements `Sum<&i32>`
`i32` implements `Sum`
note: the method call chain might not have had the expected associated types
--> src/main.rs:25:39
|
25 | let safe_reports: Vec<_>= reports.into_iter().map(|report| is_safe(report)).collect();
| ------- ^^^^^^^^^^^ ----------------------------- `Iterator::Item` changed to `bool` here
| | |
| | `Iterator::Item` is `Vec<i32>` here
| this expression has type `Vec<Vec<i32>>`
26 | safe_reports.iter().sum()
| ------ `Iterator::Item` is `&bool` here
note: required by a bound in `std::iter::Iterator::sum`
--> /home/dennis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:3578:12
|
3575 | fn sum<S>(self) -> S
| --- required by a bound in this associated function
...
3578 | S: Sum<Self::Item>,
| ^^^^^^^^^^^^^^^ required by this bound in `Iterator::sum`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `submarine` (bin "submarine") due to 1 previous error
Was ich noch zugeben muss, `safe_reports` hat den Typ `Vec<_>` weil es in einer vorherigen Fehlermeldung als Vorschlag kam, ich konnte noch nicht rausfinden wieso. Meine Vermutung liegt zur Zeit darin, dass da ein `map`-Objekt zurück kommt. Aber so ganz weiß ich das nicht. Das wäre sehr hilfreich wenn ihr mir das auch erklären könnte.
Vielen Dank und Grüße
Dennis