mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 12:39:42 +00:00
update readme.md
This commit is contained in:
@ -27,7 +27,7 @@ fn print_char(c : char) {
|
||||
}
|
||||
```
|
||||
|
||||
### Bool
|
||||
### 布尔
|
||||
🌟
|
||||
```rust, editable
|
||||
|
||||
@ -53,7 +53,7 @@ fn main() {
|
||||
```
|
||||
|
||||
|
||||
### Unit type
|
||||
### 单元类型
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
|
||||
|
@ -1 +1,59 @@
|
||||
# Statements and Expressions
|
||||
|
||||
### Examples
|
||||
```rust,editable
|
||||
fn main() {
|
||||
let x = 5u32;
|
||||
|
||||
let y = {
|
||||
let x_squared = x * x;
|
||||
let x_cube = x_squared * x;
|
||||
|
||||
// This expression will be assigned to `y`
|
||||
x_cube + x_squared + x
|
||||
};
|
||||
|
||||
let z = {
|
||||
// The semicolon suppresses this expression and `()` is assigned to `z`
|
||||
2 * x;
|
||||
};
|
||||
|
||||
println!("x is {:?}", x);
|
||||
println!("y is {:?}", y);
|
||||
println!("z is {:?}", z);
|
||||
}
|
||||
```
|
||||
|
||||
### exercises
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
// make it work with two ways: both modify the inner {}
|
||||
fn main() {
|
||||
let v = {
|
||||
let mut x = 1;
|
||||
x += 2
|
||||
};
|
||||
|
||||
assert_eq!(v, 3);
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let v = (let x = 3);
|
||||
|
||||
assert!(v == 3);
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {}
|
||||
|
||||
fn sum(x: i32, y: i32) -> i32 {
|
||||
x + y;
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user