Rust
Beginner
1 min read
Standard Library Traits: From, Into, Iterator
Example
// From / Into
#[derive(Debug)]
struct Celsius(f64);
#[derive(Debug)]
struct Fahrenheit(f64);
impl From<Celsius> for Fahrenheit {
fn from(c: Celsius) -> Self {
Fahrenheit(c.0 * 9.0 / 5.0 + 32.0)
}
}
// Custom iterator
struct Counter {
count: u32,
max: u32,
}
impl Counter {
fn new(max: u32) -> Self { Counter { count: 0, max } }
}
impl Iterator for Counter {
type Item = u32;
fn next(&mut self) -> Option<u32> {
if self.count < self.max {
self.count += 1;
Some(self.count)
} else {
None
}
}
}
fn main() {
// From / Into
let boiling = Celsius(100.0);
let f: Fahrenheit = boiling.into(); // uses Into (auto-impl from From)
println!("100°C = {:?}", f);
let freezing = Fahrenheit::from(Celsius(0.0));
println!("0°C = {:?}", freezing);
// Custom iterator — all the adapter methods are available for free
let counter = Counter::new(5);
let sum: u32 = counter.sum();
println!("sum 1..=5 = {sum}");
let pairs: Vec<_> = Counter::new(3)
.zip(Counter::new(3).skip(1))
.collect();
println!("zipped pairs: {:?}", pairs);
// Iterator pipeline on a Vec
let words = vec!["hello", "world", "rust", "is", "great"];
let long_caps: Vec<String> = words.iter()
.filter(|w| w.len() > 3)
.map(|w| w.to_uppercase())
.collect();
println!("long words: {:?}", long_caps);
}