[][src]Macro lab_grader::prompt

macro_rules! prompt {
    ( $msg:expr, $type:ty ) => { ... };
}

Calls prompt, then tries to parse the input into the provided type. If parsing fails, it will print an error message and then quit the current process.

This method trims whitespace on the beginning and end of the input string.

If you wish to deal with the error yourself instead of quitting, then call prompt, then parse yourself.

Example

#[macro_use] extern crate lab_grader;
use lab_grader::helpers;
use std::net::Ipv4Addr;

fn main() {
    let string = prompt!("Enter a string: ", String);
    println!("{}", string);

    let number = prompt!("Enter a number: ", u32);
    println!("{}", number);

    // This will exit with an error message if they
    // don't enter a valid IP
    let another = prompt!("Enter an IP: ", Ipv4Addr);
    println!("{}", another);
}

They input:

Enter a string: Here's a string
Here's a string
Enter a number: 123
123
Enter another number: not a number
Could not parse input