Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understanding Rust #36

Open
Proteusiq opened this issue Jan 20, 2023 · 1 comment
Open

Understanding Rust #36

Proteusiq opened this issue Jan 20, 2023 · 1 comment
Assignees

Comments

@Proteusiq
Copy link
Owner

struct Pistol {
    kind: String,
    country: String,
    capacity: u8,
    weight: f32, // in kg
}

enum Weapon {
    Beretta(Pistol), // kind 418 by Italian  8 (7+1) rounds weigh 0.31 kg
    Walther(Pistol), //       PPK by German pistol 8 rounds weigh 0.59 kg
}

#[derive(Debug)]
struct Agent {
    name: String,
    target: u8,
    active: bool,
}

impl Agent {
    fn report(&self, extra: u8) -> String {
        let target = if self.active {
            self.target + extra
        } else {
            self.target
        };

        format!(
            "Hello, {name}! You got {target} targets to elimate.",
            name = self.name,
            target = target
        )
    }

    fn weapon(&self, weapon: Weapon) -> String {
        match weapon {
            Weapon::Beretta(p) => format!("{name} selected Beretta {pistol}. Made in {country}, weigh {weight} kg., with {capacity} rounds."
                                        ,name=self.name, pistol=p.kind, country=p.country, weight=p.weight, capacity=p.capacity),
            Weapon::Walther(p) => format!("Oh, that is a {country} made Walter {pistol}, Agent {name}! It weighs {weight} and has {capacity} rounds."
                                        ,name=self.name, pistol=p.kind, country=p.country, weight=p.weight, capacity=p.capacity
            ),
        }
    }
}


fn main() {
    let agent = Agent {
        name: String::from("James Bond"),
        target: 7,
        active: true,
    };
    println!("{:?}", agent.report(2));

    let beratta = Weapon::Beretta(Pistol {
        kind: String::from("418"),
        country: String::from("Italy"),
        capacity: 8,
        weight: 0.31,
    });

    let walther = Weapon::Walther(Pistol {
        kind: "PPK".to_string(),
        country: "Germany".to_string(),
        capacity: 8,
        weight: 0.59,
    });

    for pistol in [beratta, walther] {
        println!("{}", agent.weapon(pistol))
    }
}
@Proteusiq Proteusiq self-assigned this Jan 20, 2023
@Proteusiq
Copy link
Owner Author

use std::fmt::{Display, Formatter, Result};


#[derive(Debug)]
struct Complex {
    real: f32,
    imag: f32,
    
}


impl Display for Complex {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "{real} + {imag}i", real=self.real, imag=self.imag)
    }
 
}


fn main() {

    
    let complex = Complex { real: 4.2, imag: 6.7 };
    println!("equation:\n {}", complex);
    println!("\n {:#?}", complex);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant