Created
June 19, 2018 14:02
-
-
Save rust-play/88c2d7892202b1c816683dc53c582c8d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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
pub trait GetInt { | |
fn get_int(&self) -> f32; | |
} | |
pub struct MagicLinux; | |
impl GetInt for MagicLinux { | |
fn get_int(&self) -> f32 { | |
5.0 | |
} | |
} | |
pub struct MagicWindows; | |
impl GetInt for MagicWindows { | |
fn get_int(&self) -> f32 { | |
std::f32::INFINITY | |
} | |
} | |
pub struct Fallback; | |
impl GetInt for Fallback { | |
fn get_int(&self) -> f32 { | |
0.0 | |
} | |
} | |
fn cross_platform_for_sure() -> impl GetInt { | |
#[cfg(windows)] | |
return MagicWindows; | |
#[cfg(unix)] | |
return MagicLinux; | |
#[cfg(not(any(unix, windows)))] | |
return Fallback; | |
} | |
fn main() { | |
println!( | |
"My 100% crossplatform integer value: {}", | |
cross_platform_for_sure().get_int() | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment