sunface/rust-by-practice

GitHub: sunface/rust-by-practice

通过大量分级示例和在线可运行练习帮助开发者系统性掌握 Rust 语言的学习平台。

Stars: 14133 | Forks: 1157

English | 中文

通过具有挑战性的示例、练习和项目来实践 Rust

[![Stars Count](https://img.shields.io/github/stars/sunface/rust-by-practice?style=flat)](https://github.com/sunface/rust-by-practice/stargazers) [![studyrut](https://img.shields.io/badge/RustCn-orange)](https://hirust.cn) [![LICENSE](https://img.shields.io/badge/license-CC_BY_4.0-green?style=flat)](https://github.com/sunface/rust-by-practice/blob/master/LICENSE)
本书旨在帮助读者轻松入门并熟练掌握 Rust。使用起来非常简单,你所需要做的就是让每个练习都能编译通过,没有错误(ERRORS)和恐慌(Panics)! ## 在线阅读 - [https://practice.rs](https://practice.rs) ## 特性 我们的部分示例和练习借鉴了 [Rust By Example](https://github.com/rust-lang/rust-by-example),感谢他们的杰出贡献! 虽然它们非常棒,但我们有自己的秘密武器 :) - 每章包含三个部分:示例、练习和实践 - 除了示例,我们还提供了`大量的练习`,你可以在线阅读、编辑和运行它们 - 涵盖了 Rust 的几乎所有方面,例如 async/await、线程、同步原语、优化、标准库、工具链、数据结构和算法等 - 每个练习都有对应的解答 - 整体难度稍高,从简单到超难:简单 🌟 中等 🌟🌟 困难 🌟🌟🌟 超难 🌟🌟🌟🌟 **我们要做的就是填补学习与实际项目开发入门之间的空白。** ## 本地运行 我们使用 [mdbook](https://rust-lang.github.io/mdBook/) 构建练习。你可以按照以下步骤在本地运行: - 克隆仓库 ``` $ git clone https://github.com/sunface/rust-by-practice ``` - 使用 Cargo 安装 mdbook ``` $ cargo install mdbook ``` - 英文版 ``` $ cd rust-by-practice && mdbook serve en/ ``` - 中文版 ``` $ cd rust-by-practice && mdbook serve zh-CN/ ``` ## 部分练习 🌟🌟🌟 元组结构体(Tuple struct)看起来类似于元组,它通过结构体名称增加了额外的含义,但没有命名字段。当你想给整个元组起一个名字,但不在乎字段的名称时,这会非常有用。 ``` // 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); } ``` 🌟🌟 在单个变量的解构中,移动和引用模式的绑定可以同时使用。这样做会导致变量的部分移动(partial move),这意味着变量的一部分会被移动,而其他部分则保留。在这种情况下,之后就不能整体使用父变量了,但是那些仅仅被引用(而非移动)的部分仍然可以使用。 ``` // 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); } ``` 🌟🌟 匹配守卫(match guard)是在 match 分支的模式之后指定的额外 if 条件,除了模式匹配之外,该条件也必须匹配,该分支才会被选中。 ``` // 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 => (), } } ```
标签:mdbook, RustCn, Rust语言, 代码示例, 可视化界面, 后端开发, 在线编译, 学习资源, 实战教程, 并发编程, 开源书籍, 异步编程, 技能提升, 数据分析, 算法与数据结构, 系统编程, 编程练习, 计算机科学, 防御加固