function with generic parameters

This commit is contained in:
Vincent Batts 2023-01-14 20:23:04 -05:00
parent f7e268c839
commit 45a76f19ff
34 changed files with 11 additions and 20 deletions

View file

@ -1,24 +1,13 @@
fn main() {
let mut rect = Rectangle {
width: 10,
height: 5,
};
println!("old area: {}", rect.area());
rect.inc_width(5);
println!("new area: {}", rect.area());
println!("coin toss: {}", pick_one("heads", "tails"));
println!("cash prize: {}", pick_one(500, 1000));
println!("cash prize: {}", pick_one(5.0, 1000));
}
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
return self.width * self.height;
}
fn inc_width(&mut self, delta: u32) {
self.width += delta;
fn pick_one<T>(a: T, b: T) -> T {
if std::process::id() % 2 == 0 {
return a
} else {
return b
}
}