26 lines
398 B
Rust
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());
|
|
} |