Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 14, 2024 02:20
Show Gist options
  • Save rust-play/a8a99b066019b17b0cc8ea7b6265b7af to your computer and use it in GitHub Desktop.
Save rust-play/a8a99b066019b17b0cc8ea7b6265b7af to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![warn(clippy::pedantic, clippy::nursery)]
#![forbid(unsafe_code)]
use num_integer::Integer; // 0.1
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum S {
F,
B,
FB,
}
fn fb<T: Integer>(n: &T) -> Option<S> {
let n3 = T::one() + T::one() + T::one();
let n5 = T::one() + T::one() + T::one() + T::one() + T::one();
let m3 = n.is_multiple_of(&n3);
let m5 = n.is_multiple_of(&n5);
if m3 && m5 {
return Some(S::FB);
}
if m3 {
return Some(S::F);
}
if m5 {
return Some(S::B);
}
None
}
#[must_use]
const fn fbu(n: usize) -> Option<S> {
if n % 15 == 0 {
return Some(S::FB);
}
if n % 3 == 0 {
return Some(S::F);
}
if n % 5 == 0 {
return Some(S::B);
}
None
}
fn main() {
for i in 0..u8::MAX {
match fb(&i) {
Some(s) => println!(
"{}zz",
match s {
S::F => "Fi",
S::B => "Bu",
S::FB => "FizzBu",
}
),
_ => println!("{i}"),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment