🦀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!
Question 0. Preparation
Section titled “Question 0. Preparation”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:
cargo new een1097_tutorial1Open the project folder in VS Code.
Question 1. Variables and Mutability
Section titled “Question 1. Variables and Mutability”Question: Your first task is to get familiar with how Rust handles variables.
- Declare an immutable variable named
laptopsand assign it the value5. - Declare a mutable variable named
phonesand assign it the value10. - Try to change the value of
laptopsto6. What happens? (Add a comment in your code to explain). - Change the value of
phonesto12. - Print the final value of
phones.
Question 2. Shadowing
Section titled “Question 2. Shadowing”Question: Shadowing allows you to declare a new variable with the same name as a previous variable.
- Declare a variable named
current_userand assign it the string"guest". - Print the current user.
- Inside a new scope (using curly braces
{}), shadowcurrent_userby declaring a new variable with the same name and assigning it the string"admin". - Print the user’s status from inside this new scope.
- After the inner scope ends, print
current_useragain.
Question 3. Scalar Data Types
Section titled “Question 3. Scalar Data Types”Question: Declare and initialize four variables, each with a different scalar type:
- An integer representing a temperature of
-5degrees. - A floating-point number representing a GPA of
3.8. - A Boolean representing whether a student is active (
true). - A character for the grade
'A'.
Print each variable with a descriptive message.
Question 4. Compound Type: Tuples
Section titled “Question 4. Compound Type: Tuples”Question: Tuples are great for grouping a fixed number of items with mixed types.
- Create a tuple named
student_datathat contains aString, au32(for age), and anf32(for height in meters). Example:"Derek",21,1.80. - Use a
letstatement to destructure the tuple into three separate variables:name,age, andheight. - Print each of the new variables.
Question 5. Compound Type: Arrays
Section titled “Question 5. Compound Type: Arrays”Question: Arrays have a fixed length and must contain elements of the same type.
- Declare an array named
monthsthat holds the first three months of the year as string slices (&str). - Access the second element of the array (
"February") using its index. - Print the second month.
Question 6. Functions
Section titled “Question 6. Functions”Question: Functions are the primary way to organize code in Rust.
- Write a function named
is_eventhat takes one parameter of typei32. - The function should return a
bool: trueif the number is even, andfalseotherwise. (Hint: use the modulo%operator). - Call this function from
mainwith both an even and an odd number and print the results.
Question 7. Ownership and Moves
Section titled “Question 7. Ownership and Moves”Question: This exercise demonstrates Rust’s core ownership concept.
- Create an owned
Stringnameds1with the value"hello". - Create a second variable
s2and assigns1to it (let s2 = s1;). - Try to print
s1after the assignment. Add a comment explaining the compile-time error you see. - Print
s2to show that ownership has been transferred.
Question 8. Borrowing and References
Section titled “Question 8. Borrowing and References”Question: To use a value without taking ownership, you can create a reference to it. This is called borrowing.
- Create a
Stringnamedmy_string. - Write a function
print_string_lengththat takes an immutable reference to aString(&String) as a parameter. - Inside the function, print the length of the string. The function should not return anything.
- Call this function from
main, passing a reference tomy_string. - After the function call, print
my_stringagain inmainto prove that you still have ownership of it.
Question 9. Structs
Section titled “Question 9. Structs”Question: Structs let you create your own custom data types.
- Define a
structnamedCarwith three fields:make(aString)model(aString)year(au32)
- In
main, create an instance of theCarstruct. - Print a message describing the car, like
"I have a 2025 VW ID.4."
Question 10. Traits
Section titled “Question 10. Traits”Question: Traits are Rust’s way of defining shared behaviour, similar to interfaces.
- Define a trait named
Summarizewith a single method signature:summary(&self) -> String. - Implement the
Summarizetrait for theCarstruct you created in the previous question. - The
summarymethod should return aStringdescribing the car. - In
main, create aCarinstance and call the.summary()method on it, then print the result.
Solutions
Section titled “Solutions”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
© 2026 Derek Molloy, Dublin City University. All rights reserved.