Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

🦀Tutorial: Rust Part 1

Welcome to this introductory tutorial on Rust! This guide is designed to give you a hands-on start with some of Rust’s core concepts, from variables and data types to the foundational ownership system.

Work through the 10 exercises below. Each question includes a sample solution and an explanation in the associated video. Please try this first before watching the video.

Good luck!


First create a new project using the VS Code IDE and then use the cargo tool to create a new project, for example, in the c:\ directory.

Use the terminal in this directory to create a project for the tutorial — for example:

Terminal window
cargo new een1097_tutorial1

Open the project folder in VS Code.


Question: Your first task is to get familiar with how Rust handles variables.

  1. Declare an immutable variable named laptops and assign it the value 5.
  2. Declare a mutable variable named phones and assign it the value 10.
  3. Try to change the value of laptops to 6. What happens? (Add a comment in your code to explain).
  4. Change the value of phones to 12.
  5. Print the final value of phones.

Question: Shadowing allows you to declare a new variable with the same name as a previous variable.

  1. Declare a variable named current_user and assign it the string "guest".
  2. Print the current user.
  3. Inside a new scope (using curly braces {}), shadow current_user by declaring a new variable with the same name and assigning it the string "admin".
  4. Print the user’s status from inside this new scope.
  5. After the inner scope ends, print current_user again.

Question: Declare and initialize four variables, each with a different scalar type:

  1. An integer representing a temperature of -5 degrees.
  2. A floating-point number representing a GPA of 3.8.
  3. A Boolean representing whether a student is active (true).
  4. A character for the grade 'A'.

Print each variable with a descriptive message.


Question: Tuples are great for grouping a fixed number of items with mixed types.

  1. Create a tuple named student_data that contains a String, a u32 (for age), and an f32 (for height in meters). Example: "Derek", 21, 1.80.
  2. Use a let statement to destructure the tuple into three separate variables: name, age, and height.
  3. Print each of the new variables.

Question: Arrays have a fixed length and must contain elements of the same type.

  1. Declare an array named months that holds the first three months of the year as string slices (&str).
  2. Access the second element of the array ("February") using its index.
  3. Print the second month.

Question: Functions are the primary way to organize code in Rust.

  1. Write a function named is_even that takes one parameter of type i32.
  2. The function should return a bool: true if the number is even, and false otherwise. (Hint: use the modulo % operator).
  3. Call this function from main with both an even and an odd number and print the results.

Question: This exercise demonstrates Rust’s core ownership concept.

  1. Create an owned String named s1 with the value "hello".
  2. Create a second variable s2 and assign s1 to it (let s2 = s1;).
  3. Try to print s1 after the assignment. Add a comment explaining the compile-time error you see.
  4. Print s2 to show that ownership has been transferred.

Question: To use a value without taking ownership, you can create a reference to it. This is called borrowing.

  1. Create a String named my_string.
  2. Write a function print_string_length that takes an immutable reference to a String (&String) as a parameter.
  3. Inside the function, print the length of the string. The function should not return anything.
  4. Call this function from main, passing a reference to my_string.
  5. After the function call, print my_string again in main to prove that you still have ownership of it.

Question: Structs let you create your own custom data types.

  1. Define a struct named Car with three fields:
    • make (a String)
    • model (a String)
    • year (a u32)
  2. In main, create an instance of the Car struct.
  3. Print a message describing the car, like "I have a 2025 VW ID.4."

Question: Traits are Rust’s way of defining shared behaviour, similar to interfaces.

  1. Define a trait named Summarize with a single method signature: summary(&self) -> String.
  2. Implement the Summarize trait for the Car struct you created in the previous question.
  3. The summary method should return a String describing the car.
  4. In main, create a Car instance and call the .summary() method on it, then print the result.

Here are the video solutions — please do not watch these solutions without having attempted the questions first. Please note that these solutions are somewhat warts and all in that I make errors and correct them live without edits.

Alternatively, you can view the video directly on YouTube: https://www.youtube.com/watch?v=G-8Kp-QS-Ak