Closed jplatte closed 3 years ago
Interesting, i'll dig into this
@jplatte
Republished the crate.
Now function looks like this
Can you check this out again?
fn get_cargo_crates() -> usize {
let cargo_installs = match exc("cargo install --list") {
Ok(installs) => installs.stdout,
Err(_) => "not present".as_bytes().to_vec(),
};
let cargo_installs = std::str::from_utf8(&cargo_installs).unwrap().lines();
let mut cargo_vec: Vec<String> = Vec::new();
for line in cargo_installs {
if !line.starts_with(" ") {
cargo_vec.push(line.to_string());
}
}
return cargo_vec.len();
}
Instead of collecting all of the lines into another Vec
and counting the elements, you can just filter the iterator and count how many elements it contains:
cargo_installs.filter(|line| !line.starts_with(" ")).count()
(plus you don't need the explicit return)
Thanks i did ! (need to read Rust Book and Rust idiomatic ways like FP) That is because i've been doing lots of GoLang.
@jplatte can we close this issue and you can leave the star?
Yes, I don't see why this would need to be kept open.
Crates may install more than one binary, for example:
Counting the un-indented lines should work though.