diff --git a/Readme.md b/Readme.md index b671d1f..72e2759 100644 --- a/Readme.md +++ b/Readme.md @@ -15,9 +15,11 @@ This book is written in English and Chinese, you can pick up the language you ar - 简体中文: [https://zh.exercise.rs](https://zh.exercise.rs) ## About exercises +- Try your best to make exercise comipile with NO ERRORS! - difficulty level: easy: 🌟 medium: 🌟🌟 hard: 🌟🌟🌟 hell: 🌟🌟🌟🌟 - Everything is online: reading, editing, testing + ## Contributing We welcome all kinds of contributors, especially ones who has precious exercises. diff --git a/deploy.sh b/deploy.sh old mode 100644 new mode 100755 index e69de29..a81489d --- a/deploy.sh +++ b/deploy.sh @@ -0,0 +1,7 @@ +cd en +./deploy + +cd ../zh-CN +./deploy + +cd .. \ No newline at end of file diff --git a/en/src/variables.md b/en/src/variables.md index ee1fba4..7d9482a 100644 --- a/en/src/variables.md +++ b/en/src/variables.md @@ -1 +1,119 @@ # Variables + +### Binding and mutablity +🌟 fix the error below with least change +```rust,editable + +fn main() { + let x: i32; // uninitialized but using, ERROR ! + let y: i32; // uninitialized but also unusing, only warning + println!("{} is equal to 5", x); +} +``` + +🌟🌟 fill the blanks in code to make it compile +```rust,editable + +fn main() { + // replace __ with a variable name + let __ = 1; + __ += 2; + + println!("{} is equal to 3", x); +} +``` + +### Scope +🌟 fix the error below with least change +```rust,editable + +fn main() { + let x: i32 = 10; + { + let y: i32 = 5; + println!("The value of x is {} and value of y is {}", x, y); + } + println!("The value of x is {} and value of y is {}", x, y); +} +``` + +🌟🌟 fix the error with the knowledge you grasped +```rust,editable + +fn main() { + println!("{}, world", x); +} + +fn define_x() { + let x = "hello"; +} +``` + +### Shadowing +🌟🌟 only change `assert_eq!` to make the `println!` work(print `42` in terminal) + +```rust,editable + +fn main() { + let x: i32 = 5; + { + let x = 12; + assert_eq!(x, 5); + } + + assert_eq!(x, 12); + + let x = 42; + println!("{}", x); // Prints "42". +} +``` + +🌟🌟 remove a line in code to make it compile +```rust,editable + +fn main() { + let mut x: i32 = 1; + x = 7; + // shadowing and re-binding + let x = x; + x += 3; + + + let y = 4; + // shadowing + let y = "I can also be bound to text!"; +} +``` + +### Unused varibles +fix the warning below with : + +- 🌟 one way +- 🌟🌟 two ways + +> Note: there are two ways you can use, but none of them is removing the line `let x = 1` + +```rust,editable + +fn main() { + let x = 1; +} + +// warning: unused variable: `x` +``` + +### Destructing +🌟🌟 fix the error below with least change + +> Tips: you can use Shadowing or Mutability + +```rust,editable + +fn main() { + let (x, y) = (1, 2); + x += 2; + + assert_eq!(x, 3); + assert_eq!(y, 2); +} +``` \ No newline at end of file diff --git a/zh-CN/src/SUMMARY.md b/zh-CN/src/SUMMARY.md index e829ac1..6c71278 100644 --- a/zh-CN/src/SUMMARY.md +++ b/zh-CN/src/SUMMARY.md @@ -1,7 +1,7 @@ # Summary - [关于 exercise.rs](why-exercise.md) -- [变量绑定与解构 todo](variables.md) +- [变量绑定与解构](variables.md) - [基本类型 todo](basic-types/intro.md) - [数值类型](basic-types/numbers.md) - [字符、布尔、单元类型](basic-types/char-bool-unit.md) diff --git a/zh-CN/src/variables.md b/zh-CN/src/variables.md index ee1fba4..f9bef98 100644 --- a/zh-CN/src/variables.md +++ b/zh-CN/src/variables.md @@ -1 +1,120 @@ -# Variables +# 变量绑定与解构 + +### 绑定和可变性 +🌟 修复下面代码的错误并尽可能少的修改 + +```rust,editable + +fn main() { + let x: i32; // 未初始化,但被使用 + let y: i32; // 未初始化,也未被使用 + println!("{} is equal to 5", x); +} +``` + +🌟🌟 完形填空,让代码编译 +```rust,editable + +fn main() { + // 使用合适的变量名替代 __ + let __ = 1; + __ += 2; + + println!("{} = 3", x); +} +``` + +### 变量作用域 +🌟 修复下面代码的错误并使用尽可能少的改变 +```rust,editable + +fn main() { + let x: i32 = 10; + { + let y: i32 = 5; + println!("x 的值是 {}, y 的值是 {}", x, y); + } + println!("x 的值是 {}, y 的值是 {}", x, y); +} +``` + +🌟🌟 使用你所掌握的知识来修复错误 +```rust,editable + +fn main() { + println!("{}, world", x); +} + +fn define_x() { + let x = "hello"; +} +``` + +### 变量遮蔽( Shadowing ) +🌟🌟 只允许修改 `assert_eq!` 来让 `println!` 工作(在终端输出 `42`) + +```rust,editable + +fn main() { + let x: i32 = 5; + { + let x = 12; + assert_eq!(x, 5); + } + + assert_eq!(x, 12); + + let x = 42; + println!("{}", x); // 输出 "42". +} +``` + +🌟🌟 删除一行代码以通过编译 +```rust,editable + +fn main() { + let mut x: i32 = 1; + x = 7; + // 遮蔽且再次绑定 + let x = x; + x += 3; + + + let y = 4; + // 遮蔽 + let y = "I can also be bound to text!"; +} +``` + +### 未使用的变量 +使用以下方法来修复编译器输出的 warning : + +- 🌟 一种方法 +- 🌟🌟 两种方法 + +> 注意: 你可以使用两种方法解决,但是它们没有一种是移除 `let x = 1` 所在的代码行 + +```rust,editable + +fn main() { + let x = 1; +} + +// compiler warning: unused variable: `x` +``` + +### 变量解构 +🌟🌟 修复下面代码的错误并尽可能少的修改 + +> 提示: 可以使用变量遮蔽或可变性 + +```rust,editable + +fn main() { + let (x, y) = (1, 2); + x += 2; + + assert_eq!(x, 3); + assert_eq!(y, 2); +} +``` \ No newline at end of file diff --git a/zh-CN/src/why-exercise.md b/zh-CN/src/why-exercise.md index d9707e0..1c9c4d5 100644 --- a/zh-CN/src/why-exercise.md +++ b/zh-CN/src/why-exercise.md @@ -1,4 +1,4 @@ -# Why Exercise.rs +# 关于 exercise.rs > 英文版 [传送门](https://exercise.rs) 欢迎大家来到 `exercise.rs`,在来到这里之前不知道你有没有碰到过以下问题: