Skip to main content

Prerequisites

Before you begin, you’ll need to install Rust on your system.
Install Rust using rustup, the official Rust installer:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Follow the on-screen instructions. After installation, restart your terminal or run:
source $HOME/.cargo/env

Verify Installation

Check that Rust is installed correctly:
rustc --version
cargo --version
You should see version information for both the Rust compiler and Cargo (Rust’s package manager).
If you already have Rust installed, make sure it’s up to date: rustup update

Clone the Repository

Get the Rust Lab code on your machine:
git clone https://github.com/Ghostmaysam1/rust-lab.git
cd rust-lab

Project Structure

The repository contains three learning projects:
rust-lab/
├── 00-ownership/          # Ownership and move semantics
│   ├── Cargo.toml
│   └── src/
│       └── main.rs
├── 01-datatypes/          # Data types and type system
│   ├── Cargo.toml
│   └── src/
│       └── main.rs
└── 02-rock_paper_scissors/  # Interactive CLI game
    ├── Cargo.toml
    └── src/
        └── main.rs

Run Your First Project

Let’s start with the ownership project to understand Rust’s most distinctive feature.
1

Navigate to the project

cd 00-ownership
2

Run the project

Use Cargo to compile and run the code:
cargo run
You’ll see output demonstrating ownership and cloning:
(L-3) my name is Maysam
(L-8) my name from new variable: Maysam
(L-15) I can use my_name here: Maysam
3

Examine the code

Open src/main.rs to see how the code works:
let my_name = String::from("Maysam");
println!("(L-3) my name is {my_name}");

let new_variable = my_name;
// my_name.push_str("hi_name"); // can't use my_name here!, value moved
println!("(L-8) my name from new variable: {new_variable}");
Notice how my_name becomes unavailable after moving to new_variable. This is Rust’s ownership system in action!

Why This Matters

The ownership project demonstrates Rust’s memory safety without garbage collection:
  • Line 2-3: my_name owns the string “Maysam”
  • Line 5: Ownership moves to new_variable
  • Lines 6-7: Attempting to use my_name would cause a compile error
  • Line 12-15: Using .clone() creates a copy, allowing both variables to be used
Try uncommenting lines 6 or 7 and running cargo run again. The compiler will explain why the code is invalid!

Explore the Other Projects

Data Types Project

Navigate to the data types project and run it:
cd ../01-datatypes
cargo run
This project demonstrates Rust’s type system, including:
  • String types (&str vs String)
  • Integer types (signed and unsigned)
  • Floating-point numbers
  • Characters (including emoji! 🦀)
  • Tuples and arrays
  • Type parsing

Rock Paper Scissors Game

Try the interactive game:
cd ../02-rock_paper_scissors
cargo run
Follow the prompts to play Rock, Paper, Scissors against the computer:
Please Enter a number
1 for 🗿 Rock!
2 for 📄 Paper!
3 for ✂️  Scissors!
> 1
Victory — your move beats the computer. computer chose ✂️ Scissors
Type exit to quit the game.

Experiment with the Code

The best way to learn is by modifying the code:

Change the Values

Modify strings, numbers, or game messages to see how Rust handles changes

Break Things

Intentionally introduce errors to learn from compiler messages

Add Features

Extend the Rock Paper Scissors game with score tracking or new options

Explore Types

Try different data types in the datatypes project

Essential Commands

Here are the Cargo commands you’ll use frequently:
CommandDescription
cargo runCompile and run the project
cargo buildCompile without running
cargo build --releaseBuild optimized release version
cargo checkCheck for errors without building
cargo cleanRemove build artifacts

Troubleshooting

Make sure Rust is in your PATH. Restart your terminal or manually source the environment:
source $HOME/.cargo/env
Rust’s compiler errors are very descriptive. Read them carefully—they often include suggestions for fixing the issue.If you’re stuck, try copying the error message into a search engine or asking on the Rust Users Forum.
The Cargo.toml files specify edition = "2024". Note that Rust editions are 2015, 2018, and 2021. If you encounter edition-related errors, you may need to update the Cargo.toml to use edition = "2021" (the latest stable edition):
[package]
edition = "2021"
Make sure your Rust installation is up to date:
rustup update

Next Steps

Now that you’ve run the projects, dive deeper into the concepts:

Ownership Concepts

Deep dive into Rust’s ownership system

Data Types Guide

Comprehensive type system reference

Learning Path

Structured approach to mastering these projects

External Resources

Books, courses, and community links
Remember: the Rust compiler is your friend! Its error messages are designed to teach you. Read them carefully and you’ll learn faster.

Build docs developers (and LLMs) love