Files
simple-rust-tests/__std/misc/src/bin/trait_struct_test.rs
2024-01-07 18:26:09 +08:00

26 lines
398 B
Rust

use std::ops::Deref;
trait WithName {
fn get_name(&self) -> &str;
}
struct Human {
name: String,
}
impl Human {
fn new(name: String) -> Self {
Self { name }
}
}
impl WithName for Human {
fn get_name(&self) -> &str {
self.name.deref()
}
}
fn main() {
let with_name = Human::new("Hatter".into());
println!("With name: {}", with_name.get_name());
}