Rustlings
repository·main·Indexed 13 days ago
https://github.com/rust-lang/rustlingsA collection of small exercises designed to help developers learn Rust by practicing reading and writing code. The curriculum covers fundamental concepts including variable mutability, functions, if expressions, primitive types, vectors, move semantics, structs, enums, pattern matching, strings, the module system, hashmaps, the Option type, error handling, generics, traits, lifetimes, testing, iterators, smart pointers (Box, Rc, Cow), and threads.
What's inside Rustlings
- This exercise section covers the basic types that are directly implemented into the Rust compiler. You will work through exercises involving the most important primitive types to understand how they are used in the language.
Learn about Iterators in Rustlings
mainThe18_iteratorssection of Rustlings provides exercises designed to teach the concepts and usage of Iterators in Rust. This module covers how to create, manipulate, and consume iterators to process sequences of data efficiently.Get started with Rustlings exercises
mainRustlings provides small exercises designed to help you get used to reading and writing Rust code. It is recommended to use Rustlings in parallel with reading the official Rust Book. For detailed setup instructions, demos, and more information, visit the official website.Understand the concept of Threads
mainThreads are features that allow independent parts of a program to run simultaneously within a single process. While an operating system manages multiple processes, threads allow for concurrency within your own application's execution flow.Use conversion traits for type transformations
mainFor more complex or safe conversions, Rust uses traits located in the
std::convertmodule. These allow types to define how they should be transformed into other types. The primary traits used in Rustlings exercises are:FromandInto: Used for infallible conversions (conversions that are guaranteed to succeed).TryFromandTryInto: Used for fallible conversions (conversions that might fail and return aResult).AsRefandAsMut: Used for cheap, reference-to-reference conversions.
Use print! and println! macros for console output
mainIn Rust, you use theprint!andprintln!macros to output text to the console.println!is commonly used to print a line of text followed by a newline character, whileprint!prints text without adding a newline.Learn about Rust struct types
mainThis exercise covers the three types of structs available in Rust:
- Classic C-style structs: Named fields with specific types.
- Tuple structs: Structs where fields are unnamed and accessed by position.
- Unit structs: Structs that have no fields at all.
To complete this exercise, you must implement the correct struct definition as required by the exercise files.
Understand variable mutability in Rust
mainIn Rust, variables are immutable by default. This means once a value is bound to a name, it cannot be changed. To allow a variable's value to be modified, you must explicitly declare it as mutable by adding themutkeyword before the variable name.Understand Enums and Pattern Matching in Rust
mainRust enums allow you to define types that enumerate a set of possible values. Unlike enums in many other languages, Rust's enums are similar to algebraic data types found in functional languages (like F#, OCaml, and Haskell).
To effectively use enums, you should use Rust's pattern matching facility, which allows you to execute different code paths based on which variant of an enumeration is currently in use.
Understand the concept of Traits in Rust
mainA trait is a collection of methods that defines shared behavior. Data types can implement traits by providing definitions for the methods specified by the trait. This allows different types to be treated uniformly, especially when writing generic code.
Commonly used standard library traits include:
Clone: Provides theclonemethod for creating copies of values.Display: Enables formatted output using the{}placeholder.Debug: Enables debug-formatted output using the{:?}placeholder.
Traits are conceptually similar to interfaces in Java or abstract classes in C++.
How Rustlings solution files work
mainIn Rustlings, solution files (located in the
solutions/directory) initially contain only an emptymainfunction. Once you successfully complete an exercise, the Rustlings CLI automatically replaces the emptymainfunction with the actual solution code.Important Note: The provided solutions represent only one possible way to solve an exercise; your implementation may differ from the official solution while still being correct.
Understand the concept of Lifetimes
mainLifetimes are a mechanism used by the Rust compiler to ensure that references remain valid for as long as they are being used. They prevent dangling references by verifying that the data being referenced lives long enough for the reference to be safe.
Key concepts:
- Purpose: Lifetimes tell the compiler how to check whether references live long enough to be valid in a given situation (e.g., ensuring a return value is valid by tying its lifetime to an input parameter).
- Applicability: Lifetimes are only necessary on borrows (references). Parameters that are copied or moved are owned within their own scope and do not require lifetime annotations for external reference safety.
- Impact on Callers: Lifetime annotations are restrictive; they define requirements that the calling code must satisfy to ensure memory safety.