Skip to main content
edited tags; edited title
Link
Shepmaster
  • 8.7k
  • 27
  • 27

Read Simplified error handling when reading from stdin and parseparsing to a type

deleted 43 characters in body
Source Link
sammko
  • 123
  • 3

I've recently started learning Rust, and I've managed to whip up this function, which reads and parses a thing from stdin. I don't like it though, and I feel it could be further simplified. What do you think?

use std::io;
use std::str;

fn read_thing<T: str::FromStr>() -> Result<T, &'static str> {
    let mut buffer = String::new();
    match io::stdin().read_line(&mut buffer) {
        Ok(_)  => match buffer.trim().parse() {
            Ok(n)  => Ok(n),
            Err(_) => Err("Failed parsing input.")
        },
        Err(_) => Err("Failed reading from stdin."),
    }
}

I've recently started learning Rust, and I've managed to whip up this function, which reads and parses a thing from stdin. I don't like it though, and I feel it could be further simplified. What do you think?

use std::io;
use std::str;

fn read_thing<T: str::FromStr>() -> Result<T, &'static str> {
    let mut buffer = String::new();
    match io::stdin().read_line(&mut buffer) {
        Ok(_)  => match buffer.trim().parse() {
            Ok(n)  => Ok(n),
            Err(_) => Err("Failed parsing input.")
        },
        Err(_) => Err("Failed reading from stdin."),
    }
}

I've recently started learning Rust, and I've managed to whip up this function, which reads and parses a thing from stdin. I don't like it though, and I feel it could be further simplified. What do you think?

fn read_thing<T: str::FromStr>() -> Result<T, &'static str> {
    let mut buffer = String::new();
    match io::stdin().read_line(&mut buffer) {
        Ok(_)  => match buffer.trim().parse() {
            Ok(n)  => Ok(n),
            Err(_) => Err("Failed parsing input.")
        },
        Err(_) => Err("Failed reading from stdin."),
    }
}
added 1 character in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Rust, read Read from stdin and parse

I've recently started learning Rust, and I've managed to whip up this function, which reads and parses.. a thing from stdinstdin. I don't like it though, and I feel it could be further simplified. What do you think?

use std::io;
use std::str;

fn read_thing<T: str::FromStr>() -> Result<T, &'static str> {
    let mut buffer = String::new();
    match io::stdin().read_line(&mut buffer) {
        Ok(_)  => match buffer.trim().parse() {
            Ok(n)  => Ok(n),
            Err(_) => Err("Failed parsing input.")
        },
        Err(_) => Err("Failed reading from stdin."),
    }
}

What do you think?

Rust, read from stdin and parse

I've recently started learning Rust, and I've managed to whip up this function, which reads and parses.. a thing from stdin. I don't like it though, I feel it could be further simplified.

use std::io;
use std::str;

fn read_thing<T: str::FromStr>() -> Result<T, &'static str> {
    let mut buffer = String::new();
    match io::stdin().read_line(&mut buffer) {
        Ok(_)  => match buffer.trim().parse() {
            Ok(n)  => Ok(n),
            Err(_) => Err("Failed parsing input.")
        },
        Err(_) => Err("Failed reading from stdin."),
    }
}

What do you think?

Read from stdin and parse

I've recently started learning Rust, and I've managed to whip up this function, which reads and parses a thing from stdin. I don't like it though, and I feel it could be further simplified. What do you think?

use std::io;
use std::str;

fn read_thing<T: str::FromStr>() -> Result<T, &'static str> {
    let mut buffer = String::new();
    match io::stdin().read_line(&mut buffer) {
        Ok(_)  => match buffer.trim().parse() {
            Ok(n)  => Ok(n),
            Err(_) => Err("Failed parsing input.")
        },
        Err(_) => Err("Failed reading from stdin."),
    }
}
Source Link
sammko
  • 123
  • 3
Loading