Rust input/output: Difference between revisions

From wikinotes
No edit summary
No edit summary
Line 8: Line 8:
</blockquote><!-- Documentation -->
</blockquote><!-- Documentation -->


= user input =
= Stdin, Stdout, Stderr =
<blockquote>
<syntaxhighlight lang="rust">
println!("out");  // write to stdout
eprintln!("err");  // write to stderr
</syntaxhighlight>
 
read from stdin (wait indefinitely if nothing is written)
<syntaxhighlight lang="rust">
let mut input: Vec<String> = Vec::new();
let stdin = io::stdin();
let handle = stdin.lock();
for result in handle.lines() {
    match result {
        Ok(line) => input.push(line),
        Err(_) => break,
    }
}
print!("START\n{}\nFINISH", input.join("\n"));
</syntaxhighlight>
</blockquote><!-- Stdin, Stdout, Stderr -->
 
= User Input =
<blockquote>
<blockquote>
<syntaxhighlight lang="rust">
<syntaxhighlight lang="rust">

Revision as of 05:22, 9 February 2023

Documentation

std::fs https://doc.rust-lang.org/std/fs/index.html

Stdin, Stdout, Stderr

println!("out");   // write to stdout
eprintln!("err");  // write to stderr

read from stdin (wait indefinitely if nothing is written)

let mut input: Vec<String> = Vec::new();
let stdin = io::stdin();
let handle = stdin.lock();
for result in handle.lines() {
    match result {
        Ok(line) => input.push(line),
        Err(_) => break,
    }
}
print!("START\n{}\nFINISH", input.join("\n"));

User Input

let name = String::new();
io::stdin().read_line(&mut name);

Files