rustHow to pretty print a struct in Rust
Pretty printing a struct in Rust is done using the #[derive(Debug)]
annotation. This annotation allows the struct to be printed in a human-readable format.
Example code
#[derive(Debug)]
struct Person {
name: String,
age: u8
}
fn main() {
let person = Person {
name: String::from("John"),
age: 30
};
println!("{:?}", person);
}
Output example
Person { name: "John", age: 30 }
Code explanation
#[derive(Debug)]
: This annotation is used to enable pretty printing of the struct.println!("{:?}", person)
: This macro is used to print the struct in a human-readable format.
Helpful links
Related
- Example of struct private field in Rust
- Example of struct with vector field in Rust
- Example of Rust struct with closure
- How to convert struct to bytes in Rust
- Example of struct public field in Rust
- How to write struct to json file in Rust
- How to convert struct to protobuf in Rust
- Example of struct of structs in Rust
- How to map struct in Rust
More of Rust
- How to get a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to perform matrix operations in Rust?
- How to split a string by regex in Rust?
- How to parse JSON string in Rust?
- How to get an entry from a HashSet in Rust?
- How to modify an existing entry in a Rust HashMap?
- How to create a subslice from a Rust slice?
- How to ignore case in Rust regex?
- How to get all matches from a Rust regex?
See more codes...