Rust:- Data Structure – struct – print – debug implementation ( fmt::Debug )

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

Link

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
Print {:?} {0:?} 0 means the first variable
Pretty Print {:#?} {1:#?} 1 means the second variable

 

Code

Original


/*
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:-

  1. #[derive(Debug)]
    • Prefix the data type with the #[derive(Debug)] keyword
  2. impl Debug for Person
    • Add an implementation keyword

 

Revision

Outline

  1. Enable Debug implementation
    • #[derive(Debug)]

Enable Debug Implementation

Code


/*
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:-

  1. #[derive(Debug)]
    • Prefix the data type with the #[derive(Debug)] keyword
    • We remove the comments that prefixed the #[derive(Debug)] keyword
  2. Format Specifiers
    • {:?}
      • Print
    • {:#?}
      • Pretty Print

Online Software Development

OnlineGDB

  1. rustdatastructurestructprintdebug.rs
    Link

 

References

  1. Rust By Example

One thought on “Rust:- Data Structure – struct – print – debug implementation ( fmt::Debug )

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s