3

I'm quite surprised I can't seem to navigate rust's documentation to find any case that describes io, could someone please explain to me how to use basic io to get user input into say, an integer? And maybe where to find the io details in that accursed documentation? Thanks

dtolnay
  • 9,621
  • 5
  • 41
  • 62
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177

2 Answers2

8

To answer your question about ints. (All of these type annotations are optional, and I've separated things out each step.)

use std::io;

fn main() {
    let mut stdin = io::stdin();

    let err_line: io::IoResult<String> = stdin.read_line();
    let line: String = err_line.unwrap();

    let line_no_extra_whitespace: &str = line.as_slice().trim();
    let possible_number: Option<int> = from_str(line_no_extra_whitespace);

    match possible_number {
        Some(n) => println!("double your number is {}", 2 * n),
        None => println!("please type an integer")
    }
}

Documentation (NB. almost all types in the docs are clickable, taking you to a page with more description/listing what you can do with them):

Also, note that the docs are searchable, via the search box at the top of the page, e.g. searching for "stdin". (You can press 's' on any page to jump to the search box, ready to type.)


You might also be interested in this answer about the difference between the heap allocated String and the string slice &str.

Others have pointed out the cheatsheet, the entry point for the docs std, and the IO-specific std::io. There's other places with nice information, like the std::result text, for working with the return values from IO operations (remember IoResult is a Result and so supports all those operations), and the #rust IRC channel on irc.mozilla.org (web client) normally has multiple people willing to help.

Community
  • 1
  • 1
huon
  • 94,605
  • 21
  • 231
  • 225
2

From http://doc.rust-lang.org/std/index.html:

Common types of I/O, including files, TCP, UPD, pipes, Unix domain sockets, timers, and process spawning, are defined in the io module.

And linked from that doc: http://doc.rust-lang.org/std/io/

Read:

use std::io;

for line in io::stdin().lines() {
    print!("{}", line.unwrap());
}
Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
  • well yes, but what about storing io into a variable rather than printing? I don't see any documentation on the type of `line`, or anything other than printing something – Syntactic Fructose Jun 17 '14 at 02:37
  • Your question made it out like you couldn't even find what I posted. Have you seen this? http://doc.rust-lang.org/complement-cheatsheet.html – Mike Cheel Jun 17 '14 at 02:43
  • 1
    @SyntacticFructose, `io::stdin()` returns a [`BufferedReader`](http://doc.rust-lang.org/std/io/struct.BufferedReader.html) which has [`lines()`](http://doc.rust-lang.org/std/io/struct.BufferedReader.html#method.lines) method due to [`Buffer`](http://doc.rust-lang.org/std/io/trait.Buffer.html) trait impl. `lines()` method returns [`Lines`](http://doc.rust-lang.org/std/io/struct.Lines.html) structure which implements iterator protocol, that is, it is an iterator returning items of type `IoResult`. Iterators can be used in `for` loops, which you undoubtedly know from the tutorial. – Vladimir Matveev Jun 17 '14 at 08:41