1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::fmt::Debug;
use std::io::{BufRead, stderr, stdin, stdout, Write};
use std::str::FromStr;

/// 標準入力ストリームから値を読み込みます。
///
/// # Examples
///
/// ```
/// // You can have rust code between fences inside the comments
/// // If you pass --test to `rustdoc`, it will even test it for you!
/// use scantool::stdin::scan;
/// let n: i32 = scan();
/// ```
pub fn scan<T: FromStr>() -> T
    where T::Err: Debug {
    let stdout: Option<_> = stdout()
        .flush()
        .ok();
    let stderr: Option<_> = stderr()
        .flush()
        .ok();

    let parse: fn(String) -> Option<T> = |x| x.trim()
        .parse()
        .ok();
    let stdin: Option<T> = stdin()
        .lock()
        .lines()
        .next()
        .and_then(|r| r.ok())
        .and_then(parse);

    stdout.and(stderr)
        .and(stdin)
        .unwrap()
}


#[test]
fn scan_test() {
    assert_eq!(2 + 2, 4);
}