What's a good iter
alternative to
use std::str;
pub struct NamedPoint {
pub metric_name: String
}
impl NamedPoint {
fn parse_datagram_line(line: &str) -> Result< NamedPoint, String > {
let parts : Vec<&str> = line.split(" ").collect();
if parts.len() != 3 {
return Err( format!("Datagram `{}` does not have 3 parts", line) );
}
let metric_name = parts[0].to_string();
Ok(NamedPoint{ metric_name: metric_name })
}
}
fn main() {
let datagram = "collectd.xle.xle-forwarder-01.disk-vda.disk_octets.read nan 1442949342\r\ncollectd.xle.xle-forwarder-01.disk-vda.disk_octets.write nan 1442949342\r\n";
let parsed_lines_iter = datagram.lines_any().map(|x| NamedPoint::parse_datagram_line(x) );
if parsed_lines_iter.any(|x| x.is_err() ) {
println!("oh no! bad parse!");
} else {
println!("yay parses worked");
}
}
Fails with:
<anon>:23:8: 23:25 error: cannot borrow immutable local variable `parsed_lines_iter` as mutable
<anon>:23 if parsed_lines_iter.any(|x| x.is_err() ) {
^~~~~~~~~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101
Play: Rust Playground
where parsed_lines_iter
is not required to be mut
? I don't need the state of the iterator or to access the passing iter item. Should I use a simple gated value with a for loop?
let mut has_it = false;
for entry in collection {
if entry.is_err() {
has_it = true;
break;
}
}