Background
In Rust, let us cover how to print out the contents of the struct data structure.
In this post we will use the fmt::Debug trait.
Definition Statement
Rust-Lang.org
Debug
All types which want to use std::fmt formatting traits require an implementation to be printable.
Automatic implementations are only provided for types such as in the std library.
All others must be manually implemented somehow.
The fmt::Debug trait makes this very straightforward.
All types can derive (automatically create) the fmt::Debug implementation.
This is not true for fmt::Display which must be manually implemented.
Formatting Code
Type | Format Specifier | Sample | Sample Explanation |
---|---|---|---|
{:?} | {0:?} | 0 means the first variable | |
Pretty Print | {:#?} | {1:#?} | 1 means the second variable |
Code
Original
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Specify that structure implements the Debug trait | |
i.e. we will be able to print structure contents by using {:?} specifier in our print statements | |
*/ | |
//#[derive(Debug)] | |
struct Person | |
{ | |
name: String | |
, age: u8 | |
} | |
fn main() | |
{ | |
// Create struct with field init shorthand | |
//String Literals ( static ) | |
let nameHardCoded = "Peter 1"; | |
let age1 = 27; | |
let objPeter1 = Person | |
{ | |
name:nameHardCoded.to_string() | |
, age:age1 | |
}; | |
//Debug Basic Print Person 1 | |
println! | |
( | |
"{0:?}" | |
, objPeter1 | |
); | |
println!(""); | |
println!(""); | |
//Debug Pretty Print Person 1 | |
println! | |
( | |
"{0:#?}" | |
, objPeter1 | |
); | |
println!(""); | |
println!(""); | |
/* | |
//Print Person 1 along with other data | |
println! | |
( | |
"variable contents \r\n \r\n {0:#?} \r\n \r\n variable name {1} \r\n \r\n age {2}" | |
, objPeter1 | |
, "objPeter1" | |
, 30 | |
); | |
*/ | |
} |
Output
Output – Image
Output – Text
error[E0277]: `Person` doesn't implement `Debug` --> main.rs:113:11 | 113 | , objPeter2 | ^^^^^^^^^ `Person` cannot be formatted using `{:?}` | = help: the trait `Debug` is not implemented for `Person` = note: add `#[derive(Debug)]` to `Person` or manually `impl Debug for Person` = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to previous error For more information about this error, try `rustc --explain E0277`
Explanation
The format string {:?} relies on the Debug trait being implemented.
Here are the option for implementing it:-
- #[derive(Debug)]
- Prefix the data type with the #[derive(Debug)] keyword
- impl Debug for Person
- Add an implementation keyword
Revision
Outline
- Enable Debug implementation
- #[derive(Debug)]
Enable Debug Implementation
Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Specify that structure implements the Debug trait | |
i.e. we will be able to print structure contents by using {:?} specifier in our print statements | |
*/ | |
#[derive(Debug)] | |
struct Person | |
{ | |
name: String | |
, age: u8 | |
} | |
fn main() | |
{ | |
// Create struct with field init shorthand | |
//String Literals ( static ) | |
let nameHardCoded = "Peter 1"; | |
let age1 = 27; | |
let objPeter1 = Person | |
{ | |
name:nameHardCoded.to_string() | |
, age:age1 | |
}; | |
//Debug Basic Print Person 1 | |
println! | |
( | |
"{0:?}" | |
, objPeter1 | |
); | |
println!(""); | |
println!(""); | |
//Debug Pretty Print Person 1 | |
println! | |
( | |
"{0:#?}" | |
, objPeter1 | |
); | |
println!(""); | |
println!(""); | |
} |
Output
Output – Image
Explanation
The format string {:?} and {:#?} relies on the Debug trait being implemented.
Here are the option for implementing it:-
- #[derive(Debug)]
- Prefix the data type with the #[derive(Debug)] keyword
- We remove the comments that prefixed the #[derive(Debug)] keyword
- Format Specifiers
- {:?}
- {:#?}
- Pretty Print
- {:?}
Online Software Development
OnlineGDB
- rustdatastructurestructprintdebug.rs
Link
[…] Rust:- Data Structure – struct – print – debug implementation ( fmt::Debug ) Link […]