Skip to main content
1 of 2
Boiethios
  • 600
  • 2
  • 11

I would slightly change this code:

use std::error::Error;

fn main() -> Result<(), Box<Error>> {
    // Args arrangement
    let mut args = std::env::args().skip(1);
    assert_eq!(args.len(), 3, "Arguments must be: file_location width height");

    // Reading args
    let file_location = args.next().unwrap();
    let width = args.next().unwrap().parse()?;
    let height = args.next().unwrap().parse()?;

    // Do the job
    let img = image::open(&file_location)?;
    img.thumbnail(width, height);

    // All was ok
    Ok(())
}
  1. You can take advantage of the try operator ? to make the error handling easier.

  2. Similarly, you can add a bit of error handling with the assert_eq line. You can use the len method because this iterator knows exactly how much elements it has.

  3. You can use skip to discard the first element of the Args iterator.

Boiethios
  • 600
  • 2
  • 11