How to print structures and arrays in Rust? (Debug, Display, formatting)
Hi everyone! I'm new to Rust and can't figure out how to output complex data types. I have a simple `User` struct and an array of numbers. When I try to print them using `println!`, the compiler complains and gives errors. Here's my code:
```rust
struct User {
name: String,
age: u32,
}
fn main() {
let user = User { name: "Alice".to_string(), age: 30 };
println!("User: {}", user); // Error!
let numbers = [1, 2, 3, 4, 5];
println!("Array: {}", numbers); // Also error!
}
```
The compiler says something about `std::fmt::Display` and `Debug`. I googled it, but it's all too complicated. I just need to output data to see its contents, not write a bunch of code. How to do this correctly and quickly? In other languages (Python, JavaScript) this works out of the box.
No answers yet.
Log in to leave an answer
Blogs