Rust 快读

· · 个人记录

Rust 版快读代码:

fn getchar() -> char {
    let mut tmp = [1];
    let res = stdin().read(&mut tmp);
    if let Ok(_) = res {
        tmp[0] as char
    } else {
        '\0'
    }
}

fn read() -> i32 {
    let mut f = true;
    let mut x = 0;
    let mut c = getchar();
    while !c.is_digit(10) && c != '\0' {
        f ^= c == '-';
        c = getchar();
    }

    while c.is_digit(10) {
        x = (x << 1) + (x << 3) + (c as u8 ^ 48) as i32;
        c = getchar();
    }

    if f { x } else { -x }
}