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

Rust Practice

Lecture 9: Wednesday, February 11, 2026.

This module gives you some practice creating, editing, and running Rust projects.

Step 1: Create the Project

We will create a project from the terminal / command line. Use Git Bash if you are on windows.

  1. Open a terminal, and navigate to the folder where you would like your project to appear using cd.

  2. Run cargo new example to create a new project. In this case, we named this project example, but you can change the name to whatever you feel like.

  3. Confirm the project was created correctly by navigating to its folder using cd example and running it using cargo run.

  4. Use pwd to the exact location where you created the folder.

You should see a similar output to the screenshot below, but the location of your project may be different.

terminal screenshot

Step 2: Open the project with VSCode

Open your VSCode, then use the file menu to click on open folder. You need to use open folder and not open.

vscode 1

Then navigate to the location from step (4) above where you placed the project, and open the project’s folder.

vscode 2

Finally, use the left navigation panel to look inside src and open file main.rs to see the code. You should see the content of the file open, as well as a run button right on top of the main function. Click run to confirm it works successfully.

vscode 3

If you do not see the run button, double check that (1) rust analyzer is installed in VSCode, and (2) you used open folder and opened the correct folder – e..g, the example folder on your desktop.

Step 3: Modify and run the code

Change the code to the following:

fn main() {
    let x = 10;
    let y = 15;
    let sum = 10 + 15;
    println!("{x} + {y} = {sum}!");
}

If you followed all the steps correctly, you will see VSCode add in the type of x, y, and sum automatically on your behalf, as shown in the screenshot below.

You can run the code from the command line using cargo run, and you can also run it from VSCode as below.

vscode 4

Step 4: Work on our first in class activity on Gradescope

For Wednesday, February 11, we will work on Lecture 9: Option, HashMap, and match practice which you can find on Gradescope.

Erase all the code from src/main.rs and, instead, copy the provided code from Gradescope to src/main.rs.

You will see an error in VScode around the map variable being immutable. Remember what we learned in class about how to fix that error!

Then, continue to the remaining two questions.

Step 5: Work on our second in class activity on Gradescope

For Friday, February 13, we will work on Lecture 10: loops and conditionals which you can find on Gradescope.

Erase all the code from src/main.rs and replace it with the provided code from Gradescope.

Add the missing code to solve the problem.