function with generic parameters
This commit is contained in:
parent
f7e268c839
commit
45a76f19ff
34 changed files with 11 additions and 20 deletions
27
src/main.rs
27
src/main.rs
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue