use std::fmt::{Debug, Display};
pub struct User {
pub name: String,
pub age: u32,
}
impl User {
pub fn new(name: String, age: u32) -> Self {
Self { name, age }
}
pub fn get_name(&self) -> &str {
&self.name
}
}
impl Debug for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("User")
.field("name", &self.name)
.field("age", &self.age)
.finish()
}
}
impl Display for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "User {{ name: {}, age: {} }}", self.name, self.age)
}
}
impl Clone for User {
fn clone(&self) -> Self {
User {
name: self.name.clone(),
age: self.age,
}
}
}
impl Default for User {
fn default() -> Self {
User {
name: String::from("Anonymous"),
age: 0,
}
}
}
pub struct Product {
pub id: u64,
pub name: String,
pub price: f64,
}
impl Product {
pub fn new(id: u64, name: String, price: f64) -> Self {
Self { id, name, price }
}
}
impl Display for Product {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Product #{}: {} (${:.2})",
self.id, self.name, self.price
)
}
}
impl Debug for Product {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Product")
.field("id", &self.id)
.field("name", &self.name)
.field("price", &self.price)
.finish()
}
}
fn main() {
let user = User::new("Alice".to_string(), 30);
println!("User: {}", user);
println!("Debug: {:?}", user);
let product = Product::new(1, "Laptop".to_string(), 999.99);
println!("Product: {}", product);
println!("Debug: {:?}", product);
}