Part 1. Guessing game (15 points)

The first application you will write is a number guessing game.


The tutorial you’re going to be following for this part of the lab contains a discussion of the integer types like i32, i64, and u32. It talks about how many bits each of them is, 32 or 64, respectively. You’ll learn a lot more about this in CSCI 210. To relate this to Java, an i32 is Java’s int type. An i64 is Java’s long type. The u32 and u64 types don’t have analogues in Java. These are the unsigned types meaning that they can only hold non-negative values. This is perhaps best demonstrated with an example. We can print out the maximum and minimum values these types can hold as follows.

fn main() {
println!("An i32 holds values from {} to {}", i32::MIN, i32::MAX);
println!("An i64 holds values from {} to {}", i64::MIN, i64::MAX);
println!("An u32 holds values from {} to {}", u32::MIN, u32::MAX);
println!("An u64 holds values from {} to {}", u64::MIN, u64::MAX);

Click the Run button to see the results. There are other integer types: i8, i16, u8, u16, isize, and usize. You can get their minimum or maximum values the same way.

Your task

Follow the instructions given in Chapter 2 of the book with the following adjustments:

  • In the section “Setting Up a New Project,” it tells you to go to a projects directory. Instead, cd into the assignment repository you cloned. You’ll create guessing_game in there when you run cargo new guessing_game

    It’s important that you do this from inside the assignment repository. Don’t run cargo new guessing_game and then later mv the directory into the assignment repo.

    After you run cargo new guessing_game, open up Visual Studio Code by running

    $ code guessing_game

    This will open Visual Studio Code which will ask you some questions:

    1. Do you trust the authors of the code? Yes.
    2. It detects that the containing directory is the root of a Git repository and it will ask if you want to open it. Say yes and then select the path to the assignment directory from the list displayed (there will only be one entry). Now, you can use Git directly from VS Code’s user interface.
  • When it tells you to run cargo run, you can do that from your open terminal window (after $ cd guessing_game), or you can use VS Code’s built-in terminal to run it.

    Let’s configure VS Code to be able to build, run, and debug our code. Click on the Run and Debug button (shaped like a triangle with a bug on it) on the left side of the window. Click the “create a launch.json file” text. It will ask you for a debugger, select LLDB. This asks if you want to generate launch configurations for its targets. Say yes.

    You can close the launch.json file it created and opened.

    At this point, you can run and debug your application from VS Code.

Once you have finished writing the guessing_game, you should make sure you’ve added your files to Git and committed them. You can do so either from the command line, or from within VS Code. Here are the docs for how to do it from within VS Code.