Rust input/output: Difference between revisions

From wikinotes
No edit summary
Line 20: Line 20:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- print -->
</blockquote><!-- print -->
= user input =
<blockquote>
<syntaxhighlight lang="rust">
let name = String::new();
io::stdin().read_line(&mut name);
</syntaxhighlight>
</blockquote><!-- input -->

Revision as of 22:02, 6 February 2023

print

println!("abc");

// string interpolation
println!("Hello, {}", "alex");
println!("{0}, {0}, see {1}?", "alex", "movie");
println!("{person}, see {thing}?", person="alex", thing="movie");

// formatting
println!("{:>4}", 2)             // "   2"  right align
println!("{:0>4}", 2)            // "0002"  right align, padded w/ zeros
println!("{var:>4}", var="boo")  // " boo"  right align

// type formatting
println!("{:X}", 1234)           // "4D2"   hex
println!("{:o}", 1234)           // "2322"  octal

user input

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