How to resolve the algorithm FizzBuzz step by step in the Rust programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm FizzBuzz step by step in the Rust programming language

Table of Contents

Problem Statement

Write a program that prints the integers from   1   to   100   (inclusive).

But:

The   FizzBuzz   problem was presented as the lowest level of comprehension required to illustrate adequacy.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm FizzBuzz step by step in the Rust programming language

Source code in the rust programming language

fn main() {
    for i in 1..=100 {
        match (i % 3, i % 5) {
            (0, 0) => println!("fizzbuzz"),
            (0, _) => println!("fizz"),
            (_, 0) => println!("buzz"),
            (_, _) => println!("{}", i),
        }
    }
}

use std::borrow::Cow;

fn main() {
    (1..=100)
        .map(|n| match (n % 3, n % 5) {
            (0, 0) => "FizzBuzz".into(),
            (0, _) => "Fizz".into(),
            (_, 0) => "Buzz".into(),
            _ => Cow::from(n.to_string()),
        })
        .for_each(|n| println!("{:?}", n));
}

use std::fmt::Write;

fn fizzbuzz() -> String {
    (1..=100).fold(String::new(), |mut output, x| {
        let fizz = if x % 3 == 0 { "fizz" } else { "" };
        let buzz = if x % 5 == 0 { "buzz" } else { "" };
        if fizz.len() + buzz.len() != 0 {
            output + fizz + buzz + "\n"
        } else {
            write!(&mut output, "{}", x).unwrap();
            output + "\n"
        }
    })
}

fn main() {
    println!("{}", fizzbuzz());
}

#![no_std]
#![feature(asm, lang_items, libc, no_std, start)]
 
extern crate libc;
 
const LEN: usize = 413;
static OUT: [u8; LEN] = *b"\
    1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n\
    16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n\
    31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n\
    46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n\
    61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n\
    76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n\
    91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz\n";
 
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
    unsafe {
        asm!(
            "
            mov $$1, %rax
            mov $$1, %rdi
            mov $0, %rsi
            mov $1, %rdx
            syscall
            "
            :
            : "r" (&OUT[0]) "r" (LEN)
            : "rax", "rdi", "rsi", "rdx"
            :
        );
    }
    0
}
 
#[lang = "eh_personality"] extern fn eh_personality() {}
#[lang = "panic_fmt"] extern fn panic_fmt() {}

  

You may also check:How to resolve the algorithm XML/XPath step by step in the Python programming language
You may also check:How to resolve the algorithm Metronome step by step in the Pure Data programming language
You may also check:How to resolve the algorithm Horizontal sundial calculations step by step in the F# programming language
You may also check:How to resolve the algorithm Draw a cuboid step by step in the Java programming language
You may also check:How to resolve the algorithm Play recorded sounds step by step in the Racket programming language