Rust print: Difference between revisions
From wikinotes
(Created page with " = println = <blockquote> == String Interpolation == <blockquote> <syntaxhighlight lang="rust"> println!("abc"); </syntaxhighlight> </blockquote><!-- String Interpolation --> == Formatting == <blockquote> <syntaxhighlight lang="rust"> println!("Hello, {}", "alex"); println!("{0}, {0}, see {1}?", "alex", "movie"); println!("{person}, see {thing}?", person="alex", thing="movie"); </syntaxhighlight> </blockquote><!-- Formatting --> == Type Formatting == <blockquote> <synt...") |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= | https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html | ||
= print = | |||
<blockquote> | <blockquote> | ||
== Printing == | |||
<blockquote> | |||
<syntaxhighlight lang="rust"> | |||
println!("hello"); // print to stdout | |||
eprintln!("error"); // pritn to stderr | |||
</syntaxhighlight> | |||
</blockquote><!-- String Interpolation --> | |||
== String Interpolation == | == String Interpolation == | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="rust"> | <syntaxhighlight lang="rust"> | ||
println!(" | let a = "A"; | ||
let b = "B"; | |||
println!("{a}--{b}"); // string interpolation | |||
println!("hello, {name}", name="will"); // keyword assignment | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- String Interpolation --> | </blockquote><!-- String Interpolation --> | ||
Line 31: | Line 45: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Type Formatting --> | </blockquote><!-- Type Formatting --> | ||
== Debug/Pretty Formatting == | |||
<blockquote> | |||
https://rust-lang.github.io/rfcs/2226-fmt-debug-hex.html?highlight=format# | |||
<syntaxhighlight lang="rust"> | |||
println!("{:#?}", myvar) // pretty-print | |||
println!("{:?}", myvar) // debug info, for 'Debug' trait objects | |||
</syntaxhighlight> | |||
</blockquote><!-- Debug/Pretty Formatting --> | |||
</blockquote><!-- println --> | </blockquote><!-- println --> | ||
<syntaxhighlight lang=" | |||
= dbg! = | |||
<blockquote> | |||
The <code>dbg!</code> macro is a special print macro intended for debugging.<br> | |||
If an object implements the <code>Debug</code> 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 <code>dbg!</code> to capture the output. | |||
<syntaxhighlight lang="rust"> | |||
#[derive(Debug)] | |||
struct Foo{a: u8, b: u8} | |||
let foo = Foo{a: 1, b: 2}; | |||
dbg!(foo) // print debug info about object | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="rust"> | |||
let result = dbg!(1 + 1) // print debug info about expression | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- dbg! --> |
Latest revision as of 03:27, 9 February 2023
https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html
Printing
println!("hello"); // print to stdout eprintln!("error"); // pritn to stderrString 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