CS 245 - Programming Languages

Lab 7

Little Rust Programs

In this lab you will write a series of small programs in Rust. The intent here is simply to practice with Rust while avoiding the whole rust borrowing thing by not using any functions other than main.

Fizz Buzz

Write a program that prints the numbers 1 .. 100 except: if the number is divisible by 3 print "Fizz", if the number is divisible by 5 print "Buzz", if the number is divisible by 3 and 5 print "FizzBuzz". Car you take advantage of the fact that rust if statements can return values to have a single print statement in your fizz-buzz loop?

K to M conversion

Write a program that takes command line input from a user to convert miles to kilometers or kilometers to miles. (1 mile equals 1.609km) For instance,
        UNIX> cargo run -- 20 k
    
Recall that you can get command line args as follows
use std::env;
fn main() {
    let args : Vec = env::args().collect();
    println!("{:?}", args);
}
    
(See chapter 12 of the Rust book.)

Having completed this program, now rewrite it to prompt the user for input in a loop (rather than taking arguments from the command line). See chapter 2 of the rust book for an example of getting input from the user. Allow the user to break out of the loop by entering a "q". For instance

UNIX> cargo run
Enter units (q to quit): k
Enter distance: 20
20 kilometers is 12.42 miles
Enter units (q to quit): m
Enter distance: 20
20 miles is 32.2061191626409 kilometers
Enter units (q to quit): 20
Enter distance: 20
Got 20 which is neither k nor m
Enter units (q to quit): w
Enter distance: 20
Got w which is neither k nor m
Enter units (q to quit): q
Enter distance: q
Goodbye
    

Times Table

Write a program to produce a times table. You should write this in a function so that the size of the table produced is a parameter of the function. For example a times table for 1..3 looks like the sample below. Getting precise columnar printing in Rust is possible, but do not worry about it for this program. To print an item without a carriage return / line feed use "print" rather than "println".
           1 2 3
        1  1 2 3
        2  2 4 6
        3  3 6 9
    
Get the size of the times table from the command line

What to hand in

Work no more than 80 minutes on the above programs. If you did not complete everything, send what you have.

Send email to gtowell@brynmawr.edu with all three programs. (Or as far as you got.)