Rust print
From wikinotes
https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html
Printing
println!("abc");String Interpolation
let a = "A"; let b = "B"; println!("{a}--{b}"); // string interpolation println!("hello, {name}", name="will"); // keyword assignmentFormatting
println!("Hello, {}", "alex"); println!("{0}, {0}, see {1}?", "alex", "movie"); println!("{person}, see {thing}?", person="alex", thing="movie");Type Formatting
// 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" octalDebug/Pretty Formatting
https://rust-lang.github.io/rfcs/2226-fmt-debug-hex.html?highlight=format#
println!("{:#?}", myvar) // pretty-print println!("{:?}", myvar) // debug info, for 'Debug' trait objects
dbg!
The
dbg!
macro is a special print macro intended for debugging.
If an object implements theDebug
trait, you can use it to show
- file
- lineno
- expression
- result
It also returns the result of the expression, so you can wrap assignments in
dbg!
to capture the output.#[derive(Debug)] struct Foo{a: u8, b: u8} let foo = Foo{a: 1, b: 2}; dbg!(foo) // print debug info about objectlet result = dbg!(1 + 1) // print debug info about expression