Created
August 8, 2021 12:02
-
-
Save humamfauzi/da63752709dfaec74de0f4fe2ee8bd27 to your computer and use it in GitHub Desktop.
Collection of Data Type, Tuple and Struct
This file contains 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
fn main() { | |
let height = 1.2; | |
let width = 5.1; | |
let area = calculate_area(height, width); | |
println!("rectangle area {}", area); | |
let prop: (f32, f32) = (1.2, 5.1); | |
let area = calculate_area_tuple(prop); | |
println!("rectangle area tuple {}", area); | |
let r1 = Rect{ height: 1.2, width: 5.1, }; | |
let area = calculate_area_struct(&r1); | |
println!("rectangle area struct {}", area); | |
println!("rectangle prop {:#?}", r1); | |
println!("rectangle area struct and method {}", r1.area()); | |
println!("{}", r1.is_encapsulate(&Rect{height: 0.2, width: 1.1,})); | |
println!("{:#?}", Rect::create_square(20.)); | |
} | |
#[derive(Debug)] | |
struct Rect { | |
height: f32, | |
width: f32, | |
} | |
impl Rect { | |
fn area(&self) -> f32 { | |
self.height * self.width | |
} | |
fn is_encapsulate(&self, other :&Rect) -> bool { | |
self.height >= other.height && self.width >= other.width | |
} | |
fn create_square(edge: f32) -> Rect { | |
Rect{ width: edge, height: edge } | |
} | |
} | |
fn calculate_area_struct(rect :&Rect) -> f32 { | |
rect.height * rect.width | |
} | |
fn calculate_area(height: f32, weight: f32) -> f32 { | |
height * weight | |
} | |
fn calculate_area_tuple(prop :(f32, f32)) -> f32 { | |
prop.0 * prop.1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment