Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Part 1. Guessing game (15 points)

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

Note

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.

#![allow(unused)]
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.

Info

LLDB is a powerful debugging tool. It allows you to do things like step through code line-by-line (extremely helpful if you’re trying to isolate a specific spot where your program isn’t behaving as expected) and inspect variable values at each step.

To use the debugger in VSCode, you can add a breakpoint in your code. This tells the debugger to stop execution once it gets to the breakpoint, and that will allow you to step through each line, one-by-one, and see what happens when each line executes. To add a breakpoint, move your cursor to the left of the line numbers in VSCode. You should see a red dot; if you click the red dot, this will add a breakpoint. (You can remove the breakpoint by clicking the red dot again.)

When you’re ready to run the program with debugging, you’ll click the Run and Debug button. Once you’re in the Run and Debug menu, you can hit F5 or the green play button on the top left. Your code will then run until the breakpoint, and then it will pause. You’ll see the control menu at the top (there’ll be a triangle play button, buttons with arrows, and a red stop button).

The button to the very left (the triangle) is a Continue button; this will cause your code to execute until it’s finished. To the right is the Step Over button. Hit this if you want to execute the current line of code and pause before executing more. The next two buttons are the Step Into and Step Out buttons. If the current line of code executes a function, Step Into will step into the function, execute the first line of it, and pause. Step Out will finish executing the function and then pause.

At any point during debugging, VSCode will display all of the variables and their values on the lefthand side of the screen.

If you’re done debugging, you can let the program continue to execute, or you can hit the Stop button (the red square).

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.