66

I'm asking for the equivalent of fgets() in C.

let line = ...;
println!("You entered: {}", line);

I've read How to read user input in Rust?, but it asks how to read multiple lines; I want only one line.

I also read How do I read a single String from standard input?, but I'm not sure if it behaves like fgets() or sscanf("%s",...).

6 Answers 6

89

In How to read user input in Rust? you can see how to iterate over all lines:

use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        println!("{}", line.unwrap());
    }
}

You can also manually iterate without a for-loop:

use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    let mut iterator = stdin.lock().lines();
    let line1 = iterator.next().unwrap().unwrap();
    let line2 = iterator.next().unwrap().unwrap();
}

You cannot write a one-liner to do what you want. But the following reads a single line (and is exactly the same answer as in How do I read a single String from standard input?):

use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    let line1 = stdin.lock().lines().next().unwrap().unwrap();
}

You can also use the text_io crate for super simple input:

#[macro_use] extern crate text_io;

fn main() {
    // reads until a \n is encountered
    let line: String = read!("{}\n");
}
4
  • 8
    What about Stdin::read_line?
    – Gerstmann
    May 12, 2015 at 10:21
  • 1
    @Gerstmann: that function is rather odd to use in my opinion.
    – oli_obk
    May 12, 2015 at 11:11
  • 1
    @Gerstmann I just tried it: for some reason it's made to accept a buffer rather than return a string, so you can't compose it with other functions working on strings.
    – Hi-Angel
    Apr 19, 2019 at 20:41
  • The separate let stdin = io::stdin(); line is no longer necessary. You can just do for line in io::stdin().lock().lines() { ... }
    – mk12
    Oct 29, 2020 at 18:40
24

If you truly want the equivalent to fgets, then @Gerstmann is right, you should use Stdin::read_line. This method accepts a buffer that you have more control of to put the string into:

use std::io::{self, BufRead};

fn main() {
    let mut line = String::new();
    let stdin = io::stdin();
    stdin.lock().read_line(&mut line).unwrap();
    println!("{}", line)
}

Unlike C, you can't accidentally overrun the buffer; it will be automatically resized if the input string is too big.

The answer from @oli_obk - ker is the idiomatic solution you will see most of the time. In it, the string is managed for you, and the interface is much cleaner.

0
13

Read a single line from stdin:

    let mut line = String::new();
    std::io::stdin().read_line(&mut line)?; // including '\n'

You may remove '\n' using line.trim_end()

Read until EOF:

    let mut buffer = String::new();
    std::io::stdin().read_to_string(&mut buffer)?;

Using implicit synchronization:

use std::io;
fn main() -> io::Result<()> {
    let mut line = String::new();
    io::stdin().read_line(&mut line)?;

    println!("You entered: {}", line);
    Ok(())
}

Using explicit synchronization:

use std::io::{self, BufRead};

fn main() -> io::Result<()> {
    let stdin = io::stdin();
    let mut handle = stdin.lock();

    let mut line = String::new();
    handle.read_line(&mut line)?;

    println!("You entered: {}", line);
    Ok(())
}

If you interested in the number of bytes e.g. n, use:
let n = handle.read_line(&mut line)?;
or
let n = io::stdin().read_line(&mut line)?;

Try this:

use std::io;
fn main() -> io::Result<()> {
    let mut line = String::new();
    let n = io::stdin().read_line(&mut line)?;

    println!("{} bytes read", n);
    println!("You entered: {}", line);
    Ok(())
}

See doc

4

Starting with Rust 1.62, you can use Stdin::lines() to get one line (of input) using only one line (of code):

fn main() {
    let line = std::io::stdin().lines().next().unwrap().unwrap();
}

Although Stdin::lines() can be used for a single line, it would typically be used for getting multiple. It simplifies this use case so that it is no longer necessary to worry about lifetimes and locking:

fn main() {
    for line in std::io::stdin().lines() {
        println!("line: {}", line.unwrap());
    }
}
1

if you want to leave the for-loop at some point

use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        match line {
            Err(_) => break,    // with ^Z
            Ok(s) => println!("{}", s),
        }

    }
    println!("fin");
}
0

You can roll your own macro.

macro_rules! read_line {
  () => {
    {
      let mut str = String::new();
      std::io::stdin().read_line(&mut str).expect("!");
      str
    }
  };
}

fn main () {
  println!("What is your name?");
  let name = read_line!();
  println!("Hello {}.", name.trim());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.