Rust is like C++, it has generics.
A Generic allows a piece of code to be written once, but used for multiple types.
You’ll most likely implement:
There are more Generic Traits out there, but the 4 above are quite common
I’ll implement From
and TryInto
here as examples
From
An example of implementing From
.
enum MyBox {
Integer(i64),
Float(f64)
}
impl From<i32> for MyBox {
fn from(i: i32) -> Self {
Self::Integer(i as i64)
}
}
impl From<f32> for MyBox {
fn from(f: f32) -> Self {
Self::Float(f as f64)
}
}
fn main() {
let i: i32 = 42;
let my: MyBox = MyBox::from(i);
let f: f32 = 9.81;
let my: MyBox = MyBox::from(f);
}
Into
Please note, implementing From
can in some cases automatically implement Into
for you.
An example of implementing Into
. (Well actually I implement TryInto
)
enum MyBox {
Integer(i64),
Float(f64)
}
impl TryInto<i64> for MyBox {
type Error = bool;
fn try_into(self) -> Result<i64, Self::Error> {
match self {
Self::Integer(i) => Ok(i),
Self::Float(_) => Err(false),
}
}
}
fn main() {
let my: MyBox = MyBox::Integer(13_i64);
let i: Result<i64, bool> = my.try_into();
if let Ok(i) = i {
// ^ i64
}
let my: MyBox = MyBox::Float(9.81_f64);
let i: Result<i64, bool> = my.try_into();
if let Ok(i) = i {
// Our try_into will return Err
} else {
// Can't convert f64 Float to i64 Integer
}
}