generics and 2/3 of polymorphism code
parent
3187d49254
commit
7f9164a747
@ -0,0 +1,13 @@
|
|||||||
|
[package]
|
||||||
|
name = "Chapter4"
|
||||||
|
version = "1.0.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "generics"
|
||||||
|
path = "generics.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "polymorphism"
|
||||||
|
path = "polymorphism.rs"
|
@ -0,0 +1,86 @@
|
|||||||
|
struct Cat
|
||||||
|
{
|
||||||
|
weight: f64,
|
||||||
|
speed: f64
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Dog
|
||||||
|
{
|
||||||
|
weight: f64,
|
||||||
|
speed: f64
|
||||||
|
}
|
||||||
|
|
||||||
|
trait Animal
|
||||||
|
{
|
||||||
|
fn max_speed(&self) -> f64;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Animal for Cat
|
||||||
|
{
|
||||||
|
fn max_speed(&self) -> f64
|
||||||
|
{
|
||||||
|
self.speed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Animal for Dog
|
||||||
|
{
|
||||||
|
fn max_speed(&self) -> f64
|
||||||
|
{
|
||||||
|
self.speed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SqueakyToy
|
||||||
|
{
|
||||||
|
weight: f64
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Stick
|
||||||
|
{
|
||||||
|
weight: f64
|
||||||
|
}
|
||||||
|
|
||||||
|
trait Toy
|
||||||
|
{
|
||||||
|
fn weight(&self) -> f64;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Toy for SqueakyToy
|
||||||
|
{
|
||||||
|
fn weight(&self) -> f64
|
||||||
|
{
|
||||||
|
self.weight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Toy for Stick
|
||||||
|
{
|
||||||
|
fn weight(&self) -> f64
|
||||||
|
{
|
||||||
|
self.weight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AnimalChasingToy<A: Animal, T: Toy>
|
||||||
|
{
|
||||||
|
animal: A,
|
||||||
|
toy: T
|
||||||
|
}
|
||||||
|
|
||||||
|
trait AnimalChasesToy<A: Animal, T: Toy>
|
||||||
|
{
|
||||||
|
fn chase(&self);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Animal, T: Toy> AnimalChasesToy<A, T> for AnimalChasingToy<A, T>
|
||||||
|
{
|
||||||
|
fn chase(&self)
|
||||||
|
{
|
||||||
|
println!("chase")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
use std::ops::Mul;
|
||||||
|
|
||||||
|
fn raise_by_three<T: Mul + Copy>(x: T) -> T
|
||||||
|
where T: std::ops::Mul<Output=T>
|
||||||
|
{
|
||||||
|
x * x * x
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
struct Raiseable<T: Mul + Copy>
|
||||||
|
{
|
||||||
|
x: T
|
||||||
|
}
|
||||||
|
impl<T: Mul + Copy> std::ops::Mul for Raiseable<T>
|
||||||
|
where T: std::ops::Mul<Output=T>
|
||||||
|
{
|
||||||
|
type Output = Raiseable<T>;
|
||||||
|
fn mul(self, rhs: Self) -> Self::Output
|
||||||
|
{
|
||||||
|
Raiseable { x: self.x * rhs.x }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
|
||||||
|
raise_by_three(10);
|
||||||
|
(10 as u64).pow(3);
|
||||||
|
|
||||||
|
raise_by_three(3.0);
|
||||||
|
(3.0 as f64).powi(3);
|
||||||
|
|
||||||
|
let x = Raiseable { x: 10 as u64 };
|
||||||
|
raise_by_three(x);
|
||||||
|
//no method named pow
|
||||||
|
//x.pow(3);
|
||||||
|
|
||||||
|
let x = Raiseable { x: 3.0 as f64 };
|
||||||
|
raise_by_three(x);
|
||||||
|
//no method named powi
|
||||||
|
//x.powi(3);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue