rustbook_learning_guide/
traits_and_generics.rs1use std::ops::Mul;
4
5pub fn multiply<T>(a: T, b: T) -> T::Output
7where
8 T: Mul,
9{
10 a * b
11}
12
13pub fn trait_examples() {
14 println!("\n🎠Trait Examples");
15 println!("{}", "-".repeat(18));
16
17 trait Greet {
18 fn greet(&self) -> String;
19 }
20
21 struct Person {
22 name: String,
23 }
24
25 impl Greet for Person {
26 fn greet(&self) -> String {
27 format!("Hello, I'm {}", self.name)
28 }
29 }
30
31 let person = Person { name: "Bob".to_string() };
32 println!("{}", person.greet());
33
34 println!("5 * 3 = {}", multiply(5, 3));
36 println!("2.5 * 4.0 = {}", multiply(2.5, 4.0));
37}
38