Created
February 18, 2024 09:48
-
-
Save luabida/8b2fd342a70d1f67b0324ff43afa4752 to your computer and use it in GitHub Desktop.
[rs] Generic Types // Retangle
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
use std::marker::Copy; | |
fn main() { | |
let r1 = Retangle { | |
width: 50, | |
height: 30.5, | |
}; | |
println!("{:?}", r1.get_area()) | |
} | |
#[derive(Debug)] | |
struct Retangle<T, U> { | |
width: T, | |
height: U, | |
} | |
impl<T, U> Retangle<T, U> | |
where | |
T: Into<f64> + Copy, | |
U: Into<f64> + Copy, | |
{ | |
fn get_area(&self) -> f64 { | |
let width: f64 = self.width.into(); | |
let height: f64 = self.height.into(); | |
width * height | |
} | |
} |
Author
luabida
commented
Feb 18, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment