playground/rust/traits.rs

28 lines
462 B
Rust

trait HelloTrait {
fn hello(&self, s:String) -> String;
}
struct HelloStruct {
name: String
}
// Implementation of the trait for the struct
impl HelloTrait for HelloStruct {
fn hello(&self, s:String) -> String {
let x = format!("Hello {}, {}!", s, self.name);
x
}
}
fn main() {
let example = HelloStruct { name: String::from("John") };
println!("{}", example.hello(String::from("there")));
}
/*
Output:
:!./traits
Hello there, John!
*/