如果可以失败,从stdin读取几个整数的最简单方法是什么?

问题描述 投票:8回答:3

假设我期望一个来自stdin的3个整数的行。阅读和解析它们最简单的方法是什么?什么是Python中的a, b, c = map(int, input().split())或C中的scanf("%d %d %d", &a, &b, &c);

我提出的最好方法是:

let mut line = String::new();
io::stdin().read_line(&mut line).unwrap();
let parts: Vec<&str> = line.split_whitespace().collect();
let a: i32 = parts[0].parse().unwrap();
let b: i32 = parts[1].parse().unwrap();
let c: i32 = parts[2].parse().unwrap();

有更简单的方法吗?

rust
3个回答
6
投票

您可以使用text_io

#[macro_use] extern crate text_io;

fn main() {
    // reads until a whitespace is encountered
    let a: i32 = read!();
    let b: i32 = read!();
    let c: i32 = read!();
}

text_io 0.1.3也支持scan!宏:

let (a, b, c): (i32, i32, i32);
scan!("{}, {}, {}\n", a, b, c);

如果您想从文件或其他来源读取,您也可以在任何实现Iterator<Item=u8>的类型上使用这两个宏:

use std::io::Read;
let mut file = std::fs::File::open("text.txt").unwrap()
                                              .bytes()
                                              .map(Result::unwrap);
let x: i32 = read!("{}\n", file);

要么

let (x, y, z): (i32, i32, i32);
scan!(file => "{}, {}: {}", x, y, z);

如果编译器可以从上下文推断出那些类型,那么你可以不使用: i32s。

免责声明:我是text_io的作者。


9
投票

您可以使用scan-rules

/*!
Add this to your `Cargo.toml`, or just run with `cargo script`:

```cargo
[dependencies]
scan-rules = "0.1.1"
```
*/
#[macro_use] extern crate scan_rules;

fn main() {
    print!("Enter 3 ints: ");
    readln! {
        (let a: i32, let b: i32, let c: i32) => {
            println!("a, b, c: {}, {}, {}", a, b, c);
        }
    }
}

如果你想做一些更多涉及的事情,你可以使用多个规则和类型推断,并指定如果输入与给定的任何规则都不匹配(默认情况下为panic!s):

    readln! {
        // Space-separated ints
        (let a: i32, let b: i32, let c: i32) => {
            println!("a b c: {} {} {}", a, b, c);
        },

        // Comma-separated ints, using inference.
        (let a, ",", let b, ",", let c) => {
            let a: i32 = a;
            let b: i32 = b;
            let c: i32 = c;
            println!("a, b, c: {}, {}, {}", a, b, c);
        },

        // Comma-separated list of *between* 1 and 3 integers.
        ([let ns: i32],{1,3}) => {
            println!("ns: {:?}", ns);
        },

        // Fallback if none of the above match.
        (..line) => {
            println!("Invalid input: {:?}", line);
        }
    }

免责声明:我是scan-rules的作者。


0
投票

我是Rust的新手,所以我可能没有完全放下这一切,但我已经找到了解决方案。我发现你可以使用split_white_space将字符串中的整数放入迭代器中。然后用“.unwrap()”将它从std::option::Option<&str>中打开。使用“.parse()”解析&str并使用“.unwrap()”解包其结果。如果使用“variable_name:i32 = ...”指定了变量类型,那么你有一个int。看看我做了什么:

let mut numbers = String::new();
io::stdin()
    .read_line(&mut numbers)
    .ok()
    .expect("read error");
let mut iter = numbers.split_whitespace();
let mut x: i32 = iter.next().unwrap().parse().unwrap();
let mut y: i32 = iter.next().unwrap().parse().unwrap();
let mut n: i32 = iter.next().unwrap().parse().unwrap();
println!("{},{},{}", x, y, n);
© www.soinside.com 2019 - 2024. All rights reserved.