Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 19, 2018 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/88c2d7892202b1c816683dc53c582c8d to your computer and use it in GitHub Desktop.
Save rust-play/88c2d7892202b1c816683dc53c582c8d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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