Created
May 28, 2021 02:34
-
-
Save libchaos/5634c04a2757fd601679cfe4012c5638 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum TrafficTight { | |
Red, | |
Green, | |
Yellow, | |
} | |
impl TrafficTight { | |
fn time(&self) -> u8 { | |
match &self { | |
TrafficTight::Red => 60, | |
TrafficTight::Green => 30, | |
TrafficTight::Yellow => 10, | |
} | |
} | |
} | |
fn sum(input: &[u32]) -> Option<u32> { | |
let mut result:u32 = 0; | |
for i in input { | |
match result.checked_add(*i) { | |
None => {return None}, | |
Some(r) => result = r, | |
} | |
} | |
return Some(result); | |
} | |
trait Area { | |
type Output; | |
fn area(&self) -> Self::Output; | |
} | |
struct Circle { | |
radius: f64, | |
} | |
struct Rectangle { | |
width: f64, | |
height: f64, | |
} | |
use std::f64::consts::PI; | |
impl Area for Circle { | |
type Output = f64; | |
fn area(&self) -> Self::Output { | |
self.radius * self.radius * PI | |
} | |
} | |
impl Area for Rectangle { | |
type Output = f64; | |
fn area(&self) -> Self::Output { | |
self.width * self.height | |
} | |
} | |
fn get_area<T: Area>(graph: T)-> <T as Area>::Output { | |
return graph.area(); | |
} | |
fn main() { | |
println!("Hello, world!"); | |
let light = TrafficTight::Red; | |
println!("red is {}", light.time()); | |
let s = sum(&[1, 2, 56, 232, 45]); | |
println!("{:?}", s); | |
let circle = Circle {radius: 0.123}; | |
println!("circle area is {}", get_area(circle)); | |
let rect = Rectangle {width: 1.12, height: 2.23}; | |
println!("rect area is {}", get_area(rect)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment