Skip to content

Instantly share code, notes, and snippets.

@luabida
Created February 18, 2024 09:48
Show Gist options
  • Save luabida/8b2fd342a70d1f67b0324ff43afa4752 to your computer and use it in GitHub Desktop.
Save luabida/8b2fd342a70d1f67b0324ff43afa4752 to your computer and use it in GitHub Desktop.
[rs] Generic Types // Retangle
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
}
}
@luabida
Copy link
Author

luabida commented Feb 18, 2024

use core::convert::Into
pub fn into(self) -> T

// Converts this type into the (usually inferred) input type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment