rust-by-practice README

The project's README from github.com/origin-brain/rust-by-practice. As one markdown file: README.md.

origin-bi%2Frust-by-practice | Trendshift

Practice Rust with challenging examples, exercises and projects

This book was designed for easily diving into and getting skilled with Rust It's very easy to use. All you need to do is to make each exercise compile without ERRORS and Panics!

Reading online

Features

Part of our examples and exercises are borrowed from Rust By Example, thanks for your great works!

Although they are so awesome, we have our own secret weapons :)

What we want to do is fill in the gap between learning and getting started with real projects.

Running locally

We use mdbook building our exercises. You can run locally with below steps:

$ git clone https://github.com/sunface/rust-by-practice
$ cargo install mdbook
$ cd rust-by-practice && mdbook serve en/
$ cd rust-by-practice && mdbook serve zh-CN/

Some of our exercises

🌟🌟🌟 Tuple struct looks similar to tuples, it has added meaning the struct name provides but has no named fields. It's useful when you want give the whole tuple a name, but don't care the fields's names.


// fix the error and fill the blanks
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
fn main() {
    let v = Point(___, ___, ___);
    check_color(v);
}

fn check_color(p: Color) {
    let (x, _, _) = p;
    assert_eq!(x, 0);
    assert_eq!(p.1, 127);
    assert_eq!(___, 255);
 }

🌟🌟 Within the destructuring of a single variable, both by-move and by-reference pattern bindings can be used at the same time. Doing this will result in a partial move of the variable, which means that parts of the variable will be moved while other parts stay. In such a case, the parent variable cannot be used afterwards as a whole, however the parts that are only referenced (and not moved) can still be used.


// fix errors to make it work
#[derive(Debug)]
struct File {
    name: String,
    data: String,
}
fn main() {
    let f = File {
        name: String::from("readme.md"),
        data: "Rust By Practice".to_string()
    };

    let _name = f.name;

    // ONLY modify this line
    println!("{}, {}, {:?}",f.name, f.data, f);
}

🌟🌟 A match guard is an additional if condition specified after the pattern in a match arm that must also match, along with the pattern matching, for that arm to be chosen.


// fill in the blank to make the code work, `split` MUST be used
fn main() {
    let num = Some(4);
    let split = 5;
    match num {
        Some(x) __ => assert!(x < split),
        Some(x) => assert!(x >= split),
        None => (),
    }
}

rust-by-practice (origin-brain/rust-by-practice)

Searching Database…

What is this?A free online book of Rust exercises, examples, and practices designed to make you comfortable with Rust by writing and running real code instead of just reading theory.
Is this a book to read or a set of exercises to solve?Both: each chapter has three parts (examples, exercises, practices), and the exercises can be read, edited, and run directly online without any local setup.
How do I start working through it?Read and solve exercises directly in the browser via the hosted online book, or clone the repo and build it locally with mdbook to work offline.
What do I need to run it locally?Rust's Cargo toolchain, used to install mdbook, which builds and serves the exercise book.
How do I know when I've solved an exercise?There's no grading system — you've solved an exercise when your code compiles with no errors and doesn't panic.
Are the exercises beginner-friendly or do they get hard fast?Difficulty is rated from one star (easy) to four stars (super hard), so the material starts gentle and ramps up toward real-project-level challenges across nearly all of Rust: async/await, threads, sync primitives, standard libraries, and data structures.
Are solutions provided if I get stuck?Yes, every single exercise ships with its own solution.
How is this different from Rust By Example?Some content is borrowed from Rust By Example, but this project adds fully runnable online exercises and practices on top, going deeper than just reading examples.
Is there a Chinese version of the book?Yes, the local build instructions cover both an English edition and a Chinese edition of the book.
Size
2.8 MB
Stars
14.8K
Updated
CommitAug 24
Age
8y
Commits · 90 Days