Rust 标准库 API 输入 stdin 输出 stdout 错误输出 stderr

Rust About 608 words

stdin

使用read_line获取输入字符串。

use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read line");
    println!("{}", input);
}

stdout

use std::io;
use std::io::Write;

fn main() {
    let mut output = String::new();
    output.push_str("Hello World From Rust");

    io::stdout().write(&output.as_bytes()).expect("Failed to write out");
}

stderr

use std::io;
use std::io::Write;

fn main() {
    let mut err = String::new();
    err.push_str("Error From Rust\n");

    io::stderr().write(&err.as_bytes()).expect("Failed to write err");
}
Views: 1,234 · Posted: 2023-04-13

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh