use std::fmt::{self, Formatter, Display};
/*
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
}
/*
Rust - How to print structs and arrays?
https://stackoverflow.com/questions/30253422/how-to-print-structs-and-arrays
All types can derive (automatically create) the fmt::Debug implementation as #[derive(Debug)],
but fmt::Display must be manually implemented.
You can create a custom output:
*/
impl std::fmt::Display for Person
{
fn fmt
(
&self, f: &mut std::fmt::Formatter
) -> std::fmt::Result
{
write!
(
f
, "struct - Person ( name: \"{0}\", age: {1} )"
, self.name
, self.age
)
}
}
fn main()
{
// Create struct with field init shorthand
//String Literals ( static )
let nameHardCoded:&'static str = "Peter 1";
let age1 = 27;
let objPeter1 = Person
{
name:nameHardCoded.to_string()
, age:age1
};
//Debug Basic Print Person 1
println!
(
"{0}"
, objPeter1
);
}