casual

Struct Input

Source
pub struct Input<T> { /* private fields */ }
Expand description

An input builder.

Implementations§

Source§

impl<T> Input<T>

Source

pub fn new() -> Self

Construct a new empty Input.

Identical to Input::default().

Source

pub fn prompt<S: Into<String>>(self, prompt: S) -> Self

Set the prompt to display before waiting for user input.

Source

pub fn prefix<S: Into<String>>(self, prefix: S) -> Self

Set the prompt prefix.

Source

pub fn suffix<S: Into<String>>(self, suffix: S) -> Self

Set the prompt suffix.

Source

pub fn default(self, default: T) -> Self

Set the default value.

If set, this will be returned in the event the user enters an empty input.

Source

pub fn matches<F>(self, matches: F) -> Self
where F: Fn(&T) -> bool + 'static,

Check input values.

If set, this function will be called on the parsed user input and only if it passes will we return the value.

§Examples
let num: u32 = Input::new().matches(|x| *x != 10).get();
Source§

impl<T> Input<T>
where T: FromStr, <T as FromStr>::Err: Display,

Source

pub fn get(self) -> T

Consumes the Input and reads the input from the user.

This function uses FromStr to parse the input data.

let num: u32 = Input::new().prompt("Enter a number: ").get();
Examples found in repository?
examples/guessing_game.rs (line 19)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
    let mut num = random();
    println!("Try guess the number I am thinking of 😃 ...");
    println!("  (hint: it's between 0 and 255)\n");

    loop {
        let guess: u32 = casual::prompt("Enter your guess: ").get();

        if guess < num {
            println!("Too low!");
        } else if guess > num {
            println!("Too high!");
        } else {
            println!("You got it!");
            println!("The number was: {}\n", num);

            if casual::confirm("Do you want to play again?") {
                num = random();
            } else {
                break;
            }
        }
    }
}
Source

pub fn map<F, U>(self, map: F) -> U
where F: Fn(T) -> U,

Consumes the Input and applies the given function to it.

This function uses FromStr to parse the input data. The result is then fed to the given closure.

let value = Input::new().map(|s: String| &s.to_lowercase() == "yes");

Trait Implementations§

Source§

impl<T: Debug> Debug for Input<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Input<T>

Source§

fn default() -> Self

Construct a new empty Input.

Identical to Input::new().

Auto Trait Implementations§

§

impl<T> Freeze for Input<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for Input<T>

§

impl<T> !Send for Input<T>

§

impl<T> !Sync for Input<T>

§

impl<T> Unpin for Input<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Input<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.