跳到正文

个人记录

Rust 快读

HydrogenC约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

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 }
}
原文