rust by practice
1.0.0

英語|中文
通過具有挑戰性的例子,練習和項目練習生鏽
這本書旨在輕鬆潛入並熟練使用Rust,這很容易使用。您需要做的就是將每個練習編譯而不會出錯和恐慌!
我們的一部分例子和練習都是從Rust借用的,感謝您的出色作品!
儘管它們太棒了,但我們有自己的秘密武器:)
每一章中有三個部分:示例,練習和實踐
除示例外,我們還有a lot of exercises ,您可以在線閱讀,編輯和運行它們
涵蓋生鏽的幾乎所有方面,例如異步/等待,線程,同步基原始人,優化,標準庫,工具鏈,數據結構和算法等。
每個練習都有自己的解決方案
總體上的困難有點更高,並且從易於實現:容易?中等的 ? ?難的 ? ? ?超級硬? ? ? ?
我們要做的是填補學習和開始研究實際項目之間的差距。
感謝我們所有的貢獻者!
?特別感謝我們的英語編輯:
蕾絲 |
我們使用MDBook構建練習。您可以在以下步驟中在本地運行:
$ git clone [email protected]:sunface/rust-by-practice.git$ cargo install mdbook$ cd rust-by-practice && mdbook serve en/$ cd rust-by-practice && mdbook serve zh-CN/???元組結構看起來與元組相似,它添加了結構名稱提供但沒有命名字段。當您想給整個元組一個名字,但不在乎字段的名稱時,這很有用。
// 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 ) ;
}?在單個變量的破壞性中,可以同時使用旁移動和副引用模式綁定。這樣做將導致變量的部分移動,這意味著在其他零件停留時將移動變量的一部分。在這種情況下,本以後無法整體使用父變量,但是仍然可以使用(不移動)的零件可以使用。
// 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 ) ;
}?比賽后衛是在模式下在匹配臂中指定的額外條件,必須匹配模式匹配,才能選擇該臂。
// 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 => ( ) ,
}
}