Module A1 - Language basics
A1.1 Basic syntax
Open exercises/A1/1-basic-syntax
in your editor. This folder contains a number of exercises with which you can practise basic Rust syntax.
While inside the exercises/A1/1-basic-syntax
folder, to get started, run:
cargo run --bin 01
This will try to compile exercise 1. Try and get the example to run, and continue on with the next exercise by replacing the number of the exercise in the cargo run command.
Note: you can also press the
▶️ Run|Debug
button to run the main function or tests. These buttons will appear above the main function and tests (it may take some time before they appear) TheDebug
button might give an error pop-up. This occurs when there are still compiler errors.
A1.1 Basic syntax 01
For this exercise, you need to remember the syntax for functions:
- The function boundary must always be explicitly annotated with types
- Within the function body type inference may be used
- A function that returns nothing has the return type unit (
()
) - The function body contains a series of statements optionally ending with an expression
#![allow(unused)] fn main() { fn add(a: i32, b: i32) -> i32 { a + b } fn returns_nothing() -> () { println!("Nothing to report"); } fn also_returns_nothing() { println!("Nothing to report"); } }
Some exercises, such as this one, contain unit tests. To run the test in src/bin/01.rs
, run:
cargo test --bin 01
A1.1 Basic syntax 02
This exercise contains unit tests as well. To run the test in src/bin/02.rs
, run:
cargo test --bin 02
Make sure all tests pass!
A1.1 Basic syntax 03
For this exercise, you will need to remember loop syntax, and control flow:
#![allow(unused)] fn main() { let mut x = 0; loop { if x < 5 { println!("x: {x}"); x += 1; } else { break; } } let mut y = 5; while y > 0 { y -= 1; println!("y: {y}"); } for i in [1, 2, 3, 4, 5] { println!("i: {i}"); } }
A1.1 Basic syntax 04
For this exercise, you will need to remember loop syntax, and control flow:
- Control flow expressions as a statement do not need to end with a semicolon
if they return unit (
()
) - Remember: A block/function can end with an expression, but it needs to have the correct type
#![allow(unused)] fn main() { let y = 11; // if as an expression let x = if y < 10 { 42 } else { 24 }; // if as a statement if x == 42 { println!("Foo"); } else { println!("Bar"); } }
A1.1 Basic syntax 05
For this exercise, you will need to remember how to access an individual value of an array.
A1.2 Move semantics
This exercise is adapted from the move semantics exercise from Rustlings
This exercise enables you to practise with move semantics. It works similarly to exercise A1.1
. To get started, open exercises/A1/2-move-semantics
in your editor and run:
cargo run --bin 01
For the exercises in this chapter, you will need to remember the following:
- To create a variable that can be changed later on, we use the keyword
mut
- There is always ever only one owner of a stack value
- Once the owner goes out of scope (and is removed from the stack), any associated values on the heap will be cleaned up as well
- Rust transfers ownership for non-copy types: move semantics
Make sure that all the exercises compile. For some exercises, instructions are included as doc comments at the top of the file. Make sure to adhere to them.