Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match arms: "mismatched types expected (), found integral variable"

Tags:

rust

I wrote the following code to parse a string to get the integer encoded in it, and check for errors using match. If I get an Err(e) I want to print out the error e, and return a default value.

return match t.parse::<usize>() {
    Ok(n) => n,
    Err(e) => {
        println!("Couldn't parse the value for gateway_threads {}", e);
        // Return two as a default value
        return 2;
    },
};

However, that code fails to compile, as it expects type () but got an integer:

error[E0308]: mismatched types
  --> src/main.rs:37:32
   |
37 |                         return 2;
   |                                ^ expected (), found integral variable
   |
   = note: expected type `()`
              found type `{integer}

If I remove the return of a default value I get the error expected usize but got `()`:

error[E0308]: match arms have incompatible types
  --> src/main.rs:33:24
   |
33 |                   return match t.parse::<usize>() {
   |  ________________________^
34 | |                     Ok(n) => n,
35 | |                     Err(e) => {
36 | |                         println!("Couldn't parse the value for gateway_threads {}", e); //TODO: Log this
37 | |                         //return 2;
38 | |                     },
39 | |                 };
   | |_________________^ expected usize, found ()
   |
   = note: expected type `usize`
              found type `()`
note: match arm with an incompatible type
  --> src/main.rs:35:31
   |
35 |                       Err(e) => {
   |  _______________________________^
36 | |                         println!("Couldn't parse the value for gateway_threads {}", e); //TODO: Log this
37 | |                         //return 2;
38 | |                     },
   | |_____________________^

Full code (I'm parsing an INI config file to get some of my values):

extern crate threadpool;
extern crate ini;

use std::net::{TcpListener, TcpStream};
use std::io::Read;
use std::process;
use threadpool::ThreadPool;
use ini::Ini;

fn main() {

    let mut amount_workers: usize;
    let mut network_listen = String::with_capacity(21);
    //Load INI
    {
        let conf: Ini = match Ini::load_from_file("/etc/iotcloud/conf.ini") {
            Ok(t) => t,
            Err(e) => {
                println!("Error load ini file {}", e);
                process::exit(0);
            },
        };
        let section = match conf.section(Some("network".to_owned())) {
            Some(t) => t,
            None => {
                println!("Couldn't find the network ");
                process::exit(0);
            },
        };
        //amount_workers = section.get("gateway_threads").unwrap().parse().unwrap();
        amount_workers = match section.get("gateway_threads") {
            Some(t) => {
                return match t.parse::<usize>() {
                    Ok(n) => n,
                    Err(e) => {
                        println!("Couldn't parse the value for gateway_threads {}", e);
                        // Return two as a default value
                        return 2; //ERROR HERE;
                    },
                };
            },
            None => 2, // Return two as a default value
        };
        let ip = section.get("bind_ip").unwrap();
        let port = section.get("bind_port").unwrap();
        network_listen.push_str(ip);
        network_listen.push_str(":");
        network_listen.push_str(port);
    }
}

What causes this error?

like image 344
Olof Avatar asked Jan 21 '26 03:01

Olof


1 Answers

Change

amount_workers = match section.get("gateway_threads") {
    Some(t) => {
        return match t.parse::<usize>() {
            Ok(n) => n,
            Err(e) => {
                println!("Couldn't parse the value for gateway_threads {}", e); //TODO: Log this
                return 2; //ERROR HERE; //Default value is set to 2
            }
        };
    }
    None => 2, //Default value is set to 2
};

to

amount_workers = match section.get("gateway_threads") {
    Some(t) => {
        match t.parse::<usize>() {  // No return
            Ok(n) => n,
            Err(e) => {
                println!("Couldn't parse the value for gateway_threads {}", e); //TODO: Log this
                2  // No semicolon, no return
            }
        } // No semicolon
    }
    None => 2, //Default value is set to 2
};

Not ending a statement with ; is how you return values in Rust. The return keyword is used when you want an entire function to return a value before the last line, which is why you call it an "early return".

You find more information about how Rust treats expressions here.

like image 185
Jan Hohenheim Avatar answered Jan 23 '26 21:01

Jan Hohenheim