rust - 匹配武器 : "mismatched types expected (), found integral variable"

标签 rust

我编写了以下代码来解析字符串以获取其中编码的整数,并使用 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);
    }
}

是什么导致了这个错误?

最佳答案

改变

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

不以 ; 结尾的语句是你在 Rust 中返回值的方式。当您希望整个函数在最后一行之前返回一个值时,使用 return 关键字,这就是为什么将其称为“提前返回”的原因。

您会找到更多关于 Rust 如何处理表达式的信息 here .

关于rust - 匹配武器 : "mismatched types expected (), found integral variable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45237658/

相关文章:

rust - 查找Rust Prelude类型的文档

rust - 如何处理多个嵌套的工作空间根?

rust - 了解相关的生命周期

rust - 跨任务和闭包的 DuplexStream

rust - 为什么我会收到一个简单的 trait 实现的 "overflow evaluating the requirement"错误?

rust - 在 Gentoo 上的 IntelliJ IDEA 中,由于 gentoo 不使用 rustup,我该如何附加 rust stdlib 源?

python - 在解释器或编译器的上下文中,单元格是什么?

rust - 如何将远程 crate 的枚举序列化和反序列化为数字?

vector - 在 Rust 中重复向量中元素的最佳方法是什么?

iterator - 为什么要连接我的迭代器中的字符串?