Up through conversion in RBE
This commit is contained in:
parent
7f4f627769
commit
505e04613f
18
rustbyexample/conversion.d/from_into.rs
Normal file
18
rustbyexample/conversion.d/from_into.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use std::convert::From;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Number {
|
||||||
|
value: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<i32> for Number {
|
||||||
|
fn from(item: i32) -> Self {
|
||||||
|
Number { value: item }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let num = Number::from(30);
|
||||||
|
println!("My number is {:?}", num);
|
||||||
|
}
|
19
rustbyexample/conversion.d/from_into2.rs
Normal file
19
rustbyexample/conversion.d/from_into2.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use std::convert::Into;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Number {
|
||||||
|
value: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<Number> for i32 {
|
||||||
|
fn into(self) -> Number {
|
||||||
|
Number { value: self }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let int = 5;
|
||||||
|
let num: Number = int.into();
|
||||||
|
println!("My number is {:?}", num);
|
||||||
|
}
|
16
rustbyexample/conversion.d/string.rs
Normal file
16
rustbyexample/conversion.d/string.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
struct Circle {
|
||||||
|
radius: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Circle {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Circle of radius {}", self.radius)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let circle = Circle { radius: 6 };
|
||||||
|
println!("{}", circle.to_string());
|
||||||
|
}
|
7
rustbyexample/conversion.d/string2.rs
Normal file
7
rustbyexample/conversion.d/string2.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fn main() {
|
||||||
|
let parsed: i32 = "5".parse().unwrap();
|
||||||
|
let turbo_parsed = "10".parse::<i32>().unwrap();
|
||||||
|
|
||||||
|
let sum = parsed + turbo_parsed;
|
||||||
|
println!("Sum: {:?}", sum);
|
||||||
|
}
|
28
rustbyexample/conversion.d/try_from_try_into.rs
Normal file
28
rustbyexample/conversion.d/try_from_try_into.rs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
use std::convert::TryFrom;
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct EvenNumber(i32);
|
||||||
|
|
||||||
|
impl TryFrom<i32> for EvenNumber {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(value: i32) -> Result<Self, Self::Error> {
|
||||||
|
if value % 2 == 0 {
|
||||||
|
Ok(EvenNumber(value))
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_eq!(EvenNumber::try_from(8), Ok(EvenNumber(8)));
|
||||||
|
assert_eq!(EvenNumber::try_from(5), Err(()));
|
||||||
|
|
||||||
|
let result: Result<EvenNumber, ()> = 8i32.try_into();
|
||||||
|
assert_eq!(result, Ok(EvenNumber(8)));
|
||||||
|
|
||||||
|
let result: Result<EvenNumber, ()> = 5i32.try_into();
|
||||||
|
assert_eq!(result, Err(()));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user