Let's learn Rust 01, can you spot the bug in fizzbuzz?

in #learn5 years ago

Table of Contents
─────────────────

1 So let's do something else
.. 1.1 Learn Rust, the systems programming language from Mozilla.
.. 1.2 FizzBuzz

1 So let's do something else
════════════════════════════

1.1 Learn Rust, the systems programming language from Mozilla.
──────────────────────────────────────────────────────────────

Rust was made to make a browser that will be a lot safer than current
offerings, eventual the code should make its way into firefox. There
is also some other interesting projects made in Rust, ond of which is
the micro kernel OS [Redox].

First since I am running linux I will be able to setup rust with the
following command
┌────
│ curl https://sh.rustup.rs -sSf | sh

└────

That did not work for me, but on unix based systems that should be the
way to go. Anyway I managed to install it with
┌────
│ sudo pacman -S rust

└────
Should also be fine.

Then some minor setup for my editor, so I can write rust in org-mode.

Then unto the actual code part.

[Redox] http:redox-os.org

1.2 FizzBuzz
────────────

A very simple program to start with would be FizzBuzz But first I
would like to check that everything is setup right, so a hello world.
┌────

│ fn main() {
│ println!("Hello, world");
│ }
└────

Now to compile this simple program do the following. If this works
the installation is succesful, otherwise you need to figure out what
went wrong.

┌────
│ rustc fzbz.rs
│ ./fzbz

└────

Okay now for something more interesting

rust01.png
┌────



│ fn fzbz(n:u64) {
│ if (n % 3 == 0) {
│ println!("Fizz");
│ } else if n % 5 == 0 {
│ println!("Buzz");
│ } else if n % 15 == 0 {
│ println!("FizzBuzz");
│ } else {
│ println!("{}", n);
│ }
│ }

│ fn main() {
│ for n in 1..101 {
│ fzbz(n)
│ }
│ }
└────

Can you spot the problem with this code?

Coin Marketplace

STEEM 0.19
TRX 0.18
JST 0.032
BTC 88295.05
ETH 3086.02
USDT 1.00
SBD 2.76