rustbook-learning-guide 0.1.0

A comprehensive Rust learning guide with practical examples covering ownership, traits, polymorphism, and more
Documentation
//! Traits and generics examples

use std::ops::Mul;

/// Generic multiply function that works with any type implementing Mul
pub fn multiply<T>(a: T, b: T) -> T::Output 
where 
    T: Mul,
{
    a * b
}

pub fn trait_examples() {
    println!("\n🎭 Trait Examples");
    println!("{}", "-".repeat(18));
    
    trait Greet {
        fn greet(&self) -> String;
    }
    
    struct Person {
        name: String,
    }
    
    impl Greet for Person {
        fn greet(&self) -> String {
            format!("Hello, I'm {}", self.name)
        }
    }
    
    let person = Person { name: "Bob".to_string() };
    println!("{}", person.greet());
    
    // Test the multiply function
    println!("5 * 3 = {}", multiply(5, 3));
    println!("2.5 * 4.0 = {}", multiply(2.5, 4.0));
}