Comment on What could go wrong trying to solve AoC in Rust?

m_f@midwest.social ⁨9⁩ ⁨months⁩ ago

The collect’s in the middle aren’t necessary, neither is splitting by ": ". Here’s a simpler version

fn main() {
    let text = "seeds: 79 14 55 13\nwhatever";
    let seeds: Vec<_> = text
        .lines()
        .next()
        .unwrap()
        .split_whitespace()
        .skip(1)
        .map(|x| x.parse::<u32>().unwrap())
        .collect();
    println!("seeds: {:?}", seeds);
}

It is simpler to bang out a [int(num) for num in text.splitlines()[0].split(’ ')[1:]] in Python, but just shows the happy path with no error handling, and does a bunch of allocations that the Rust version doesn’t. You can also get slightly fancier in the Rust version by collecting into a Result for better error handling and all that.

source
Sort:hotnewtop