匹配怀里:“类型不匹配预期(),发现积分变”

问题描述 投票:2回答:1

我写了下面的代码解析字符串来得到它编码的整数,并检查使用match错误。如果我得到一个Err(e)我想打印出错误e,并返回默认值。

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

如果我删除默认值的回报,我得到的错误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 | |                     },
   | |_____________________^

完整的代码(我解析INI配置文件让我的一些值):

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);
    }
}

是什么原因导致这个错误?

rust
1个回答
2
投票

更改

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
};

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
};

不是终止与;声明是你如何在锈返回值。当你想要一个全功能的最后一行,这就是为什么称它为一个“早归”之前返回值的return使用关键字。

你会发现担心生锈如何处理表达式here更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.