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…

这是什么?一本免费的 Rust 在线练习书,包含示例、练习和实践,目的是让你通过编写并运行真实代码来熟悉 Rust,而不只是阅读理论。
这是用来读的书,还是用来做的练习?两者都有:每一章分为三部分(示例、练习、实践),练习可以直接在线阅读、编辑和运行,无需任何本地设置。
我该怎么开始做这些练习?通过托管的在线版直接在浏览器中阅读并完成练习;或者克隆仓库,用 mdbook 在本地构建,离线使用。
想在本地运行,我需要准备什么?需要 Rust 的 Cargo 工具链来安装 mdbook;mdbook 负责构建并提供这本练习书的访问。
我怎么知道自己已经做对了一道练习?没有评分系统——当你的代码编译时没有错误且不会 panic,就说明你已经完成了这道练习。
这些练习对新手友好吗,还是难度上升很快?难度分为一星(简单)到四星(超难)。内容由易到难逐步提升,最终接近真实项目的挑战,几乎涵盖 Rust 的所有方面:async/await、线程、同步原语、标准库和数据结构。
卡住了有参考答案吗?有,每一道练习都附带了自己的参考答案。
这和 Rust By Example 有什么不同?部分内容借鉴自 Rust By Example,但本项目在此基础上增加了可直接在线运行的练习和实践,不只是阅读示例,而是讲得更深入。
这本书有中文版吗?有,本地构建的说明同时涵盖英文版和中文版。
Size
2.8 MB
Stars
14.8K
Updated
CommitAug 24
Age
8y
Commits · 90 Days