Skip to content

Instantly share code, notes, and snippets.

@DanielAdeniji
Last active November 26, 2022 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielAdeniji/6a35fc21f67e9cc2d52eab4c7471973e to your computer and use it in GitHub Desktop.
Save DanielAdeniji/6a35fc21f67e9cc2d52eab4c7471973e to your computer and use it in GitHub Desktop.
/*
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
);
*/
}
rustdatastructurestructprintdebug.success.rs
/*
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
);
*/
}
rustdatastructurestructprintdisplay.rs
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
, "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 age = 27;
let objPeter = Person
{
name:nameHardCoded.to_string()
, age:age
};
//Debug Basic Print Person 1
println!
(
"{0}"
, objPeter
);
//String Literals ( static )
let nameJohn = "John 2";
let objJohn = nameJohn.to_string();
let ageJohn = 29;
let objJohn = Person
{
name:objJohn
, age:ageJohn
};
//Debug Basic Print Person 1
println!
(
"{0}"
, objJohn
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment