跳转到主要内容

End of the Rust Course

Summary and final challenge

Congratulations: you have made it through the 4 modules of the Rust Course — from your very first variables and default immutability to ownership, borrowing, and implementing methods on structs. Below is a map of everything you have mastered and a three-step final challenge.

Reminder: Rust exercises are verified statically (keywords). To actually run the code, each exercise provides a Rust Playground button that copies the code and opens play.rust-lang.org.

01 · Fundamentals

  • variables
  • let / let mut / const
  • shadowing
  • primitive types
  • tuples / arrays

02 · Control flow

  • if / else
  • if as expression
  • loop
  • while
  • for and range
  • match

03 · Ownership and Borrowing

  • ownership
  • stack and heap
  • move
  • references &
  • mutable borrowing &mut

04 · Structs and Methods

  • structs
  • instances
  • impl methods
  • associated functions (new)

The final challenge, in three steps

Combine what you have learned about Rust by building three small logical components: a boolean checker, an in-place modification using pass-by-mutable-reference, and a struct that encapsulates a count value.

1 · Even/odd check

Write a function that accepts a 32-bit integer and returns true if it is even, false otherwise. Module 1 (operators) + Module 2 (functions).

锻炼#rust.boss.e1
尝试:0加载中...

Define the function is_even(n: i32) -> bool that returns a boolean value indicating if the passed number is even (n % 2 == 0).

正在加载编辑器...
显示提示

Use the return type -> bool and the modulo operator (%). Example: n % 2 == 0

3 次尝试后可用的解决方案

2 · Double and square by mutable reference

Write a function that receives a mutable reference to an integer and squares it directly in the original variable. Module 3 (pass-by-mutable-reference).

锻炼#rust.boss.e2
尝试:0加载中...

Define the function square_by_ref(n: &mut i32) that accepts a mutable reference to an integer and calculates its square in-place, dereferencing n to modify it.

正在加载编辑器...
显示提示

Inside the function write *n = *n * *n; to access and modify the value pointed to by the reference.

3 次尝试后可用的解决方案

3 · Encapsulated Counter Struct

Create a struct with private fields that provides public methods to initialize the value to 0, increment it, and read it. Module 4 (structs and methods).

锻炼#rust.boss.e3
尝试:0加载中...

Complete the implementation of Counter by defining pub fn new() -> Counter returning Counter with count at 0, pub fn increment(&mut self) incrementing count by 1, and pub fn get_count(&self) -> u32 returning count.

正在加载编辑器...
显示提示

Use the pub keyword to make the methods accessible outside of the impl block.

3 次尝试后可用的解决方案

Printable Cheatsheet

A single page with all the essential syntax of modern Rust, ready to keep handy while you code.

Open the cheatsheet

What now?

Consistent practice is the best way to strengthen your skills. Open the Rust Playground to experiment freely with complex scenarios or to prepare your code snippets before testing them on a real compiler.