How to read an integer input from the user in Rust 1.0?

后端 未结 7 1946
栀梦
栀梦 2020-11-30 11:08

Existing answers I\'ve found are all based on from_str (such as Reading in user input from console once efficiently), but apparently from_str(x) h

相关标签:
7条回答
  • 2020-11-30 11:52

    I would definitely use the file system Rust-Lang provides std::fs (See more here: https://doc.rust-lang.org/stable/std/fs/) But more particularly https://doc.rust-lang.org/stable/std/fs/fn.read_to_string.html

    Let's say you just want to read input of a text file, try this :

    use std::fs
    or
    use std::fs::read_to_string
    
    fn main() {
        println!("{}", fs::read_to_string("input.txt"));   
    }
    
    0 讨论(0)
提交回复
热议问题