Getting `expected tuple, found `()`` errors when using `imagesize::size`

I am using imagesize::size to get image width and height but getting following errors.

Code

use imagesize::size;

fn main() {
    let (width, height) = match size("image.png") {
        Ok(dim) => (dim.width, dim.height),
        Err(why) => println!("Error! {}", why)
    };
    println!("{}, {}", width, height);
}

Errors

Compiling img v0.1.0 (/home/infi/img)
error[E0308]: `match` arms have incompatible types
  --> src/main.rs:13:21
   |
11 |       let (width, height) = match size(img) {
   |  ___________________________-
12 | |         Ok(dim) => (dim.width, dim.height),
   | |                    ----------------------- this is found to be of type `(usize, usize)`
13 | |         Err(why) => println!("Error! {}", why)
   | |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected tuple, found `()`
14 | |     };
   | |_____- `match` arms have incompatible types
   |
   = note:   expected type `(usize, usize)`
           found unit type `()`
   = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0308`.
error: could not compile `img` due to previous error

Err, change the println! to panic! so it does not return

In which case you can simply use .unwrap() and save yourself the trouble of writing a match statement.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.