Skip to content

Collection of Rust Exercises to learn and practice building CLI tools in Rust

License

Notifications You must be signed in to change notification settings

unpeturbed/rust-practice

Repository files navigation

rust-practice-map

Rust Practice

This repository tracks my progress of learning Rust, focusing on building production-grade CLI tools.

The exercises are structured to cover all essential Rust concepts, from basics like variables and control flow to advanced topics like asynchronous programming and unsafe Rust and contain tiny projects that teach how to build efficient and scalable production-ready CLI apps.

This repo contains 240+ exercises.

  • First time here? Say helloπŸ‘‹πŸ» to everyone here.
  • Check out the updates & Announcements tab for latest insights.
  • If you have any questions, feel free to ask in Q&A discussion tab.

Table of Contents

  1. Folder Structure

  2. Exercises

    Basic Rust
    1. hello world
    2. variables and mutability
    3. data types
    4. control flow
    5. functions
    6. Ownership and borrowing
    Intermediate Rust
    1. structs
    2. enums
    3. collections
    4. error handling
    5. traits
    6. iterators and closures
    7. modules and crates
    Advanced Rust
    1. concurrency
    2. asynchronous programming
    3. smart pointers
    4. macros
    5. unsafe rust
    6. lifetimes and generics
    Projects
    1. command line tool
    2. advanced CLI projects
    3. package building
    4. cross-platform development
    5. integrations, plugins and systems
  3. Contributing

  4. License

  5. Acknowledgement

Folder Structure

This repo is divided into 3 types of folders.

  1. Levels
    • Level indicates the difficulty of exercises.
    • e.g. Basic, Intermediate, Advanced, Projects
  2. Topics
    • Each level contains multiple topics.
    • Each topic is a cargo workspace.
    • e.g. variables, functions, enums, traits ...
  3. Exercises
    • Each topic contains 10 exercises.
    • Each exercise is a member of that workspace.
    • e.g. variables_01, variables_02, variables_03 ...

Other Folders and Files

  1. Image Folder
    • It contains images that are used in the README.md file.
  2. Quick Links
    • Quick links is a file, that contains links to solutions for all exercises(i.e. links to src/main.rs).
β”œβ”€β”€ 01-basic-rust                           // Level
β”‚Β Β  └── 01_hello_world                      // Topic
β”‚Β Β   Β Β  β”œβ”€β”€ Cargo.lock
β”‚Β Β   Β Β  β”œβ”€β”€ Cargo.toml
β”‚Β Β   Β Β  └── hello_world_01                  // Exercise
β”‚Β Β   Β Β   Β Β  β”œβ”€β”€ Cargo.toml
β”‚Β Β   Β Β   Β Β  └── src
β”‚Β Β   Β Β   Β Β      └── main.rs                 // Solution
β”œβ”€β”€ 02-intermediate-rust
β”œβ”€β”€ 03-advanced-rust
β”œβ”€β”€ 04-projects
β”œβ”€β”€images                                   // Images Folder
β”‚Β   └── rust-practice-map.png               // Mind Map
β”œβ”€β”€ CODE_OF_CONDUCT.md
β”œβ”€β”€ CONTRIBUTING.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ QUICK_LINKS.md                          // Direct Links
β”œβ”€β”€ README.md
└── SECURITY.md

You can quickly navigate to solutions using Quick Links.

Exercises

1. Basics Rust (10%)

Hello, World! (0.5%)

  • Print "Hello, World!" to the console.
  • Modify the program to take user input and print "Hello, [name]!".
  • Create a program that prints a greeting based on the time of day.
  • Print a multi-line string using raw strings.
  • Format a string with multiple variables and print it.
  • Print a list of numbers from 1 to 10
  • Print the length of a given string.
  • Print the result of basic arithmetic operations.
  • Print a countdown from 10 to 1
  • Create a program that prints a quote using escape characters.

Variables and Mutability (1.5%)

  • Swap two variables without using a third variable.
  • Declare a constant and print its value.
  • Create a mutable variable and change its value inside a loop.
  • Shadow a variable and print both values.
  • Create a program that calculates the area of a rectangle.
  • Convert a string to an integer and print the result.
  • Declare a variable with a tuple and access its elements.
  • Create a program that doubles the value of a number using a mutable variable.
  • Implement a program that changes a variable's value based on user input.
  • Create a program that increments a counter variable each time a button is pressed.

Data Types and Casting (3%)

  • Convert a floating-point number to an integer.
  • Parse a string as a number and perform arithmetic on it.
  • Convert a number from one type to another and print the result.
  • Create a program that determines the type of a variable.
  • Implement a program that calculates the square root of a number.
  • Create a program that converts Celsius to Fahrenheit.
  • Convert a number to a string and concatenate it with another string.
  • Write a program that casts an integer to a character.
  • Convert a number from binary to decimal.
  • Create a program that calculates the area of a circle using a floating-point number.

Control Flow (5%)

  • Implement a program that checks if a number is even or odd.
  • Write a simple program that uses if-else to compare two numbers.
  • Create a calculator using a match statement.
  • Implement a program that checks whether a character is a vowel or consonant.
  • Write a program that checks if a number is positive, negative, or zero.
  • Implement a program that finds the largest of three numbers.
  • Create a program that simulates a basic traffic light system.
  • Write a program that checks if a year is a leap year.
  • Create a simple menu-driven program using a loop-and-match statement.
  • Write a program that calculates the factorial of a number using a loop.

Functions (7%)

  • Create a function that returns the square of a number.
  • Implement a function that takes two numbers and returns their sum.
  • Write a function that checks if a number is prime.
  • Implement a function that returns the nth Fibonacci number.
  • Write a function that reverses a string.
  • Create a function that checks if a string is a palindrome.
  • Implement a function that returns the maximum of two numbers.
  • Write a function that calculates the greatest common divisor (GCD) of two numbers.
  • Implement a function that calculates the sum of an array of numbers.
  • Create a function that generates a random number within a specified range.

Ownership and Borrowing (10%)

  • Create a function that takes ownership of a string and prints it.
  • Implement a function that borrows a string and returns its length.
  • Write a function that takes two borrowed strings and concatenates them.
  • Create a function that returns ownership of a string to the caller.
  • Implement a function that modifies a borrowed string using mutable references.
  • Write a program that demonstrates the use of multiple references.
  • Create a function that borrows an array and returns its sum.
  • Implement a program that shows how ownership changes when a variable is reassigned.
  • Write a function that takes ownership of a vector and returns its length.
  • Create a program that demonstrates the use of references in function parameters and return types.

2. Intermediate Rust (20%)

Structs (12%)

  • Create a struct to represent a book with fields for title, author, and pages.
  • Implement a method for the book struct to display its details.
  • Write a program that creates a struct to represent a point in 2D space and calculate the distance between two points.
  • Create a struct to represent a rectangle and calculate its area.
  • Implement a struct to represent a student with fields for name, age, and grades, and calculate the average grade.
  • Write a program that creates a struct to represent a complex number and implements methods for addition and subtraction.
  • Create a struct to represent a car with fields for make, model, and year, and implement methods to start and stop the car.
  • Implement a struct to represent a bank account with fields for account number, balance, and owner, and create methods to deposit and withdraw money.
  • Write a program that creates a struct to represent a person with fields for name, age, and address, and implement methods to update the address.
  • Create a struct to represent a circle with a field for radius, and implement methods to calculate the circumference and area.

Enums and Pattern Matching (14%)

  • Create an enum to represent traffic light states and use pattern matching to display messages.
  • Implement a program that uses an enum to represent different shapes (Circle, Square, Rectangle) and calculate their area.
  • Write a program that uses an enum to represent the result of a calculation (Success, Error) and uses pattern matching to handle both cases.
  • Create an enum to represent different types of users (Admin, Guest, Registered) and use pattern matching to display messages based on user type.
  • Implement a program that uses an enum to represent different payment methods (CreditCard, PayPal, Cash) and calculate the total cost based on the payment method.
  • Write a program that uses an enum to represent different file types (Text, Binary, Image) and use pattern matching to process files based on their type.
  • Create an enum to represent different weather conditions (Sunny, Rainy, Cloudy) and use pattern matching to display appropriate messages.
  • Implement a program that uses an enum to represent the status of an order (Pending, Shipped, Delivered) and use pattern matching to update the status.
  • Write a program that uses an enum to represent different error types (NotFound, PermissionDenied, Unknown) and use pattern matching to handle errors.
  • Create an enum to represent different operations (Add, Subtract, Multiply, Divide) and use pattern matching to implement a basic calculator.

Collections (Vectors, HashMaps) (18%)

  • Create a program that adds and removes elements from a vector.
  • Implement a program that finds the largest number in a vector.
  • Write a program that sorts a vector of integers in ascending and descending order.
  • Create a program that removes duplicate elements from a vector.
  • Implement a program that uses a HashMap to count the occurrences of words in a text.
  • Write a program that stores student grades in a HashMap and calculates the average grade.
  • Create a program that stores user preferences in a HashMap and allows updating and retrieving preferences.
  • Implement a program that uses a HashMap to group words by their first letter.
  • Write a program that uses a HashMap to store and retrieve configuration settings.
  • Create a program that uses a HashMap to implement a basic phone book with names and phone numbers.

Error Handling (21%)

  • Write a program that handles file reading errors and displays an appropriate message if the file is not found.
  • Implement a function that returns a Result type and handles both success and error cases.
  • Create a program that parses a string as an integer and handles invalid input with error messages.
  • Write a function that attempts to open a file and returns a custom error type if the file cannot be opened.
  • Implement a program that reads user input and handles errors if the input is not a valid number.
  • Create a program that uses Option to handle missing values and displays a default value if a value is None.
  • Write a program that attempts to divide two numbers and handles division by zero errors.
  • Implement a function that performs a calculation and returns a Result type with a custom error message if the calculation fails.
  • Create a program that reads a configuration file and handles errors if the file is missing or corrupted.
  • Write a function that attempts to connect to a server and returns a custom error type if the connection fails.

Traits (24%)

  • Create a trait called Printable with a method print and implement it for different types (e.g., struct, enum).
  • Write a program that creates a trait called Calculable with methods for addition, subtraction, multiplication, and division, and implement it for a struct representing a calculator.
  • Implement a trait called Drawable with a method draw and implement it for different shapes (Circle, Square, Rectangle).
  • Write a program that creates a trait called Serializable with methods for serializing and deserializing data, and implement it for a struct representing a user.
  • Implement a trait called Comparable with a method compare and implement it for different types (i32, f64, String).
  • Write a program that creates a trait called Cloneable with a method clone and implement it for a struct representing a document.
  • Implement a trait called Readable with a method read and implement it for different file types.
  • Create a trait called Filterable with a method filter that filters elements from a collection of structs representing products (Product { name, price, in_stock }).
  • Create a trait called StateTransition with a method transition that takes an charachter and based on his score change his level in GameLevel (with levels like Beginner, Intermediate, and Expert).
  • Create a trait called Loggable with a method log that takes an object and logs its state.

Iterators and Closures (28%)

  • Create a program that uses an iterator to sum all elements in a vector.
  • Implement a program that filters out even numbers from a vector using an iterator.
  • Write a program that maps a vector of integers to their squares using an iterator.
  • Create a program that uses an iterator to find the first element in a vector that satisfies a condition.
  • Implement a program that creates a custom iterator for a struct representing a range of numbers.
  • Write a program that chains multiple iterators to process a vector of numbers.
  • Create a program that uses a closure to sort a vector of integers in descending order.
  • Implement a program that uses a closure to filter a vector of strings based on their length.
  • Write a program that uses a closure to find the maximum value in a vector of integers.
  • Create a program that uses a closure to generate a sequence of numbers and returns the result as a vector.

Modules and Crates (30%)

  • Create a module to organize functions for mathematical operations and use it in a main program.
  • Implement a program that uses a module to organize functions for file handling and use it in a main program.
  • Write a program that creates a module to organize functions for string processing and use it in a main program.
  • Create a module to organize functions for error handling and use it in a main program.
  • Implement a program that uses a module to organize functions for working with collections (Vector, HashMap) and use it in a main program.
  • Write a program that creates a module to organize functions for networking (e.g., making HTTP requests) and use it in a main program.
  • Create a crate for common utilities (e.g., logging, configuration) and use it in a project.
  • Implement a program that creates a module to organize functions for working with dates and times and use it in a main program.
  • Write a program that creates a crate to provide a library of functions for data serialization (e.g., JSON, XML) and use it in a project.
  • Create a program that uses a third-party crate (e.g., serde, regex) and organizes the code into modules.

3. Advanced Rust(40%)

Concurrency (35%)

  • Write a program that uses threads to perform multiple calculations in parallel.
  • Implement a program that spawns multiple threads to process a large array and combines the results.
  • Create a program that uses channels to send messages between threads.
  • Implement a program that uses a thread pool to manage and execute multiple tasks concurrently.
  • Write a program that uses Arc (atomic reference counting) to share data between threads safely.
  • Create a program that uses Mutex to protect shared data from race conditions.
  • Implement a program that uses RwLock to allow multiple readers or one writer at a time.
  • Write a program that uses threads to download multiple files concurrently.
  • Create a program that implements a simple producer-consumer pattern using threads and channels.
  • Implement a program that uses threads to simulate a basic chat server where multiple clients can send and receive messages concurrently.

Asynchronous Programming (41%)

  • Write an asynchronous program that fetches data from a web API using async/await.
  • Implement a program that performs multiple HTTP requests concurrently using async/await.
  • Create an asynchronous program that reads and writes to a file without blocking the main thread.
  • Write a program that uses asynchronous functions to perform a long-running task in the background.
  • Implement a program that uses tokio or async-std to create an asynchronous TCP server.
  • Create an asynchronous program that simulates a delay using tokio::time::sleep or similar.
  • Write a program that performs multiple database queries concurrently using asynchronous functions.
  • Implement an asynchronous program that streams data from a source and processes it in real-time.
  • Create an asynchronous program that handles multiple client connections to a server concurrently.
  • Write a program that uses asynchronous functions to manage a pool of resources (e.g., database connections).

Smart Pointers (Box, Rc, RefCell) (47%)

  • Write a program that uses Box to create a linked list.
  • Implement a program that uses Rc to create a tree data structure with shared ownership of nodes.
  • Create a program that uses RefCell to modify data inside a struct with shared ownership.
  • Write a program that uses Box to create a recursive data structure (e.g., a binary tree).
  • Implement a program that uses Rc to share a configuration object between multiple threads.
  • Create a program that uses RefCell to implement interior mutability in a struct.
  • Write a program that uses Rc and RefCell together to create a graph data structure.
  • Implement a program that uses Box to store a trait object and call methods on it dynamically.
  • Create a program that uses Rc to manage a list of subscribers in a publish-subscribe pattern.
  • Write a program that uses RefCell to dynamically update a data structure while maintaining shared ownership.

Macros (54%)

  • Write a simple macro that prints "Hello, Macro!" to the console.
  • Implement a macro that calculates the square of a number.
  • Create a macro that generates getter and setter methods for a struct.
  • Write a macro that repeats a block of code a specified number of times.
  • Implement a macro that generates a match statement for an enum with multiple variants.
  • Create a macro that generates a function to create a new instance of a struct.
  • Write a macro that implements a trait for a struct.
  • Implement a macro that creates a vector with a specified number of elements.
  • Create a macro that performs compile-time checks on a struct field.
  • Write a macro that generates code for a custom Debug implementation for a struct.

Unsafe Rust (63%)

  • Write a program that uses raw pointers to access and modify memory.
  • Implement a program that uses unsafe code to call a C function from Rust.
  • Create a program that uses unsafe code to implement a custom memory allocator.
  • Write a program that uses unsafe code to bypass Rust's borrowing rules and modify data.
  • Implement a program that uses unsafe code to create a custom smart pointer.
  • Create a program that uses unsafe code to implement a linked list with raw pointers.
  • Write a program that uses unsafe code to optimize a performance-critical section of code.
  • Implement a program that uses unsafe code to implement a custom trait for a foreign type.
  • Create a program that uses unsafe code to directly access hardware registers.
  • Write a program that uses unsafe code to create a low-level system library.

Lifetimes and Generics (70%)

  • Write a program that uses lifetimes to ensure references are valid.
  • Implement a function that uses generics to work with multiple data types.
  • Create a program that uses lifetimes and generics together to create a flexible data structure.
  • Write a function that returns a reference with a lifetime annotation.
  • Implement a generic function that works with any type that implements a specific trait.
  • Create a program that uses lifetimes to manage references in a data structure.
  • Write a generic function that returns the larger of two values.
  • Implement a program that uses lifetimes to manage the scope of a borrowed value.
  • Create a program that uses generics to implement a custom collection type.
  • Write a program that uses lifetimes and generics to implement a flexible and reusable function.

4. Projects (30%)

Command-Line Tool (73%)

  • Create a simple CLI tool that greets the user with their name.
  • Implement a CLI tool that fetches weather information from an API.
  • Write a CLI tool that performs basic file operations (e.g., create, read, write, delete).
  • Create a CLI tool that interacts with a database to store and retrieve data.
  • Implement a CLI tool that performs basic arithmetic operations with command-line arguments.
  • Write a CLI tool that parses JSON data from a file and displays it in a readable format.
  • Create a CLI tool that downloads files from the internet and saves them locally.
  • Implement a CLI tool that converts data between different formats (e.g., CSV to JSON).
  • Write a CLI tool that manages a to-do list with add, remove, and list operations.
  • Create a CLI tool that interacts with a web API to send and receive data.

Advanced CLI Projects (78%)

  • File Searcher: Create a CLI tool that recursively searches for files by name in a directory.
  • Process Manager: Build a CLI tool to list and manage system processes (e.g., kill processes).
  • Database CRUD: Create a CLI tool that performs basic CRUD operations on a SQLite database.
  • Config File Parser: Build a CLI tool that reads and edits configuration files (e.g., JSON, TOML).
  • API Client: Create a CLI tool that interacts with a REST API to fetch and display data.
  • Log Analyzer: Build a CLI tool to parse and analyze log files, providing statistics and error reports.
  • Markdown to HTML Converter: Create a CLI tool that converts Markdown files into HTML.
  • Task Runner: Build a CLI tool that schedules and runs predefined tasks (like a simple cron job).
  • Backup Manager: Create a CLI tool to automate the backup of files to a specified location.
  • Git Helper: Build a CLI tool that wraps common Git commands to simplify version control tasks.

Package Building (85%)

  • Basic Packager: Create a CLI tool that packages a Rust project into a binary for distribution.
  • Cross-Compiler: Build a CLI tool that cross-compiles a Rust project for different platforms.
  • Installer Generator: Create a CLI tool that generates an installer for a Rust application.
  • Tarball Creator: Build a CLI tool that packages a project into a tarball with optional compression.
  • Binary Size Analyzer: Create a CLI tool that analyzes and reports the size of a compiled binary.
  • Release Packager: Build a CLI tool that automates the creation of release packages with versioning.
  • Installer Validator: Create a CLI tool that verifies the integrity and correctness of an installer.
  • Package Signer: Build a CLI tool that signs packages with a cryptographic signature.
  • Deb/RPM Package Creator: Create a CLI tool that builds a Debian or RPM package from a Rust project.
  • Multi-Platform Installer: Build a CLI tool that generates installers for multiple platforms (e.g., Windows, macOS, Linux).

Cross-Platform Development (91%)

  • Multi-OS CLI Tool: Create a CLI tool that runs on Windows, macOS, and Linux with platform-specific features.
  • Cross-Platform File Watcher: Build a CLI tool that watches for file changes on different operating systems.
  • Universal Package Manager: Create a CLI tool that works with multiple package managers (e.g., apt, brew, chocolatey).
  • Portable CLI Tool: Build a CLI tool that requires no installation and runs from a USB drive.
  • Cross-Platform Logger: Create a CLI tool that logs system events across different operating systems.
  • Environment Configuration Tool: Build a CLI tool that configures environment variables for different OSs.
  • Cross-Platform Process Monitor: Create a CLI tool that monitors system processes on multiple operating systems.
  • Universal Installer: Build a CLI tool that installs software packages across different platforms.
  • Multi-OS Backup Tool: Create a CLI tool that performs backups on different operating systems.
  • Cross-Platform Clipboard Manager: Build a CLI tool that manages the clipboard across different operating systems.

Integration, Plugins, and Systems (100%)

  • Plugin System: Create a CLI tool that supports plugins, allowing users to extend its functionality.
  • API Gateway: Build a CLI tool that acts as a gateway for integrating with multiple APIs.
  • Task Scheduler: Create a CLI tool that schedules and runs tasks at specified intervals, integrating with system cron jobs.
  • Messaging Queue Client: Build a CLI tool that interacts with a message queue system (e.g., RabbitMQ, Kafka).
  • Service Integrator: Create a CLI tool that integrates with cloud services (e.g., AWS, GCP) and performs tasks.
  • Database Migrator: Build a CLI tool that manages database migrations and integrates with different database systems.
  • CI/CD Pipeline Integrator: Create a CLI tool that integrates with CI/CD systems like Jenkins or GitHub Actions.
  • System Monitor Integrator: Build a CLI tool that integrates with system monitoring tools (e.g., Prometheus, Grafana).
  • Custom Shell: Create a CLI tool that acts as a custom shell with support for plugins and extensions.
  • Configuration Manager: Build a CLI tool that manages configuration files and integrates with other system tools.

Contributing 🀝

Contributions are welcome! If you have any suggestions, improvements, or new exercises, feel free to open an issue or submit a pull request. Let's make this repository a valuable resource for everyone learning Rust.

License πŸ“œ

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgment πŸ™

A big thank you to the Rust community and all the contributors who have helped make this repository better. Your support and feedback are greatly appreciated.

Contact πŸ“¬

For any questions or feedback, you can reach out to me on GitHub or Dev.to.

About

Collection of Rust Exercises to learn and practice building CLI tools in Rust

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages