1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#![doc = include_str!("../README.md")]
/**
# Rust `FizzBuzz` in 125 chars
This is hideous. Normally we would never want or need such minified code,
except in the [silly][1] competition known as "Code Golf".
Rust apparently allows for removing whitespace from code, to minify it in a
similar way as JavaScript, another language sometimes similar to BrainFuck
which allows us to write hideously minified code.
Running `rustfmt` on this suffers the whitespace penalty, but does make it more
readable.
[1]:
https://web.archive.org/web/20230914220048if_/https://i.pinimg.com/736x/b4/fb/cd/b4fbcdd779d811a2b2493a0d7b3929a2--fantasy-quotes-monty-python.jpg
*/
fn main(){for i in 1..101{let f=i%3<1;let b=i%5<1;if f{print!("Fizz")}if b{print!("Buzz")}if!(f|b){print!("{i}")}println!()}}
use std::print as p;
use std::println as n;
/// # Macro to shorten boolean variable definitions
///
/// This macro is designed to shorten the definition of boolean variables (`$b`)
/// to denote: "`$i` is divisible by `$x`".
///
/// Here we use `tt` (`TokenTree`) fragment matcher to keep things succinct for
/// Code Golf purposes, rather than other more appropriate syntactical fragment
/// matchers.
///
/// The equivalent generated Rust code for `l!(a,i,3)` would be:
///
/// let a=i%3<1;
///
macro_rules! l{($b:tt,$i:tt,$x:tt)=>{let$b=$i%$x<1;};}
/// # Macro to print "`Fizz`"
///
/// This macro is designed to shorten `println!("Fizz")`
macro_rules! F{()=>{p!("Fizz")};}
/// # Macro to print "`Buzz`"
///
/// This macro is designed to shorten `println!("Buzz")`
macro_rules! B{()=>{p!("Buzz")};}
/// # An even smaller `main()` function (91 chars)
///
/// Using an import alias and macros [`l`], [`F`], and [`B`], we can slim down
/// the `main()` function further. Yet, the boilerplate syntax actually makes
/// the total number of characters in the code larger.
// Smallest main() function: 91 chars
fn pain(){for i in 1..101{l!(f,i,3);l!(b,i,5);if f{F!()}if b{B!()}if!(f|b){p!("{i}")}n!()}}