commit
105e78151a
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
### Char
|
### Char
|
||||||
1. 🌟
|
1. 🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
|
|
||||||
// Make it work
|
// Make it work
|
||||||
use std::mem::size_of_val;
|
use std::mem::size_of_val;
|
||||||
|
@ -18,7 +18,7 @@ fn main() {
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 🌟
|
2. 🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
|
|
||||||
// Make it work
|
// Make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -33,7 +33,7 @@ fn print_char(c : char) {
|
||||||
|
|
||||||
### Bool
|
### Bool
|
||||||
3. 🌟
|
3. 🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
|
|
||||||
// Make println! work
|
// Make println! work
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -47,7 +47,7 @@ fn main() {
|
||||||
```
|
```
|
||||||
|
|
||||||
4. 🌟
|
4. 🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
|
|
||||||
// Make it work
|
// Make it work
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -24,7 +24,7 @@ fn main() {
|
||||||
2. 🌟
|
2. 🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// Fill the blank
|
// Fill the blank
|
||||||
fn main() {
|
fn main() {
|
||||||
let v: u16 = 38_u8 as __;
|
let v: u16 = 38_u8 as __;
|
||||||
|
|
||||||
|
|
|
@ -240,7 +240,7 @@ Besides jump into the standard library, you can also jump to another module in t
|
||||||
// in lib.rs
|
// in lib.rs
|
||||||
|
|
||||||
mod a {
|
mod a {
|
||||||
/// Add four to the given value and return a [`Option`] type
|
/// Add four to the given value and return a [`Option`] type
|
||||||
/// [`crate::MySpecialFormatter`]
|
/// [`crate::MySpecialFormatter`]
|
||||||
pub fn add_four(x: i32) -> Option<i32> {
|
pub fn add_four(x: i32) -> Option<i32> {
|
||||||
Some(x + 4)
|
Some(x + 4)
|
||||||
|
|
|
@ -335,7 +335,7 @@ The `impl Trait` syntax works for straightforward cases but is actually syntax s
|
||||||
When working with generics, the type parameters often must use traits as bounds to stipulate what functionality a type implements.
|
When working with generics, the type parameters often must use traits as bounds to stipulate what functionality a type implements.
|
||||||
|
|
||||||
7. 🌟🌟
|
7. 🌟🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(sum(1, 2), 3);
|
assert_eq!(sum(1, 2), 3);
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,7 @@ fn print_str(s: String) {
|
||||||
```
|
```
|
||||||
|
|
||||||
5. 🌟🌟
|
5. 🌟🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
// Don't use clone ,use copy instead
|
// Don't use clone ,use copy instead
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = (1, 2, (), "hello".to_string());
|
let x = (1, 2, (), "hello".to_string());
|
||||||
|
|
|
@ -204,7 +204,7 @@ fn main() {
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it
|
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it
|
|
@ -114,6 +114,6 @@ fn main() {
|
||||||
&mut value => value.push_str(" world!")
|
&mut value => value.push_str(" world!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
````
|
```
|
||||||
|
|
||||||
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it
|
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it
|
||||||
|
|
|
@ -50,20 +50,20 @@ fn main() {
|
||||||
let v2 = i8::checked_add(119, 8).unwrap();
|
let v2 = i8::checked_add(119, 8).unwrap();
|
||||||
println!("{},{}",v1,v2);
|
println!("{},{}",v1,v2);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
6.
|
6.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn main() {
|
fn main() {
|
||||||
let v = 1_024 + 0xff + 0o77 + 0b1111_1111;
|
let v = 1_024 + 0xff + 0o77 + 0b1111_1111;
|
||||||
assert!(v == 1597);
|
assert!(v == 1597);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
7.
|
7.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = 1_000.000_1; // f64
|
let x = 1_000.000_1; // f64
|
||||||
let y: f32 = 0.12; // f32
|
let y: f32 = 0.12; // f32
|
||||||
|
@ -76,7 +76,7 @@ fn main() {
|
||||||
fn type_of<T>(_: &T) -> String {
|
fn type_of<T>(_: &T) -> String {
|
||||||
format!("{}", std::any::type_name::<T>())
|
format!("{}", std::any::type_name::<T>())
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
8.
|
8.
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ fn main() {
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
4.
|
4.
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ fn main() {
|
||||||
// A non-copy type.
|
// A non-copy type.
|
||||||
let movable = Box::new(3);
|
let movable = Box::new(3);
|
||||||
|
|
||||||
// A copy type would copy into the closure leaving the original untouched.
|
// A copy type would copy into the closure leaving the original untouched.
|
||||||
// A non-copy must move and so `movable` immediately moves into
|
// A non-copy must move and so `movable` immediately moves into
|
||||||
// the closure.
|
// the closure.
|
||||||
let consume = || {
|
let consume = || {
|
||||||
|
@ -55,7 +55,7 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
consume();
|
consume();
|
||||||
// consume();
|
// consume();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn take<T>(_v: T) {
|
fn take<T>(_v: T) {
|
||||||
|
@ -68,7 +68,7 @@ fn main() {
|
||||||
// A non-copy type.
|
// A non-copy type.
|
||||||
let movable = Box::new(3);
|
let movable = Box::new(3);
|
||||||
|
|
||||||
// A copy type would copy into the closure leaving the original untouched.
|
// A copy type would copy into the closure leaving the original untouched.
|
||||||
// A non-copy must move and so `movable` immediately moves into
|
// A non-copy must move and so `movable` immediately moves into
|
||||||
// the closure.
|
// the closure.
|
||||||
let consume = || {
|
let consume = || {
|
||||||
|
|
|
@ -105,7 +105,7 @@ fn print_str(s: String) {
|
||||||
fn print_str(s: &String) {
|
fn print_str(s: &String) {
|
||||||
println!("{}",s)
|
println!("{}",s)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
5.
|
5.
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
### 字符
|
### 字符
|
||||||
1. 🌟
|
1. 🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
// 修改2处 `assert_eq!` 让代码工作
|
// 修改2处 `assert_eq!` 让代码工作
|
||||||
|
|
||||||
use std::mem::size_of_val;
|
use std::mem::size_of_val;
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -18,8 +18,8 @@ fn main() {
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 🌟
|
2. 🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
// 修改一行让代码正常打印
|
// 修改一行让代码正常打印
|
||||||
fn main() {
|
fn main() {
|
||||||
let c1 = "中";
|
let c1 = "中";
|
||||||
print_char(c1);
|
print_char(c1);
|
||||||
|
@ -32,7 +32,7 @@ fn print_char(c : char) {
|
||||||
|
|
||||||
### 布尔
|
### 布尔
|
||||||
3. 🌟
|
3. 🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
|
|
||||||
// 使成功打印
|
// 使成功打印
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -46,7 +46,7 @@ fn main() {
|
||||||
```
|
```
|
||||||
|
|
||||||
4. 🌟
|
4. 🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let f = true;
|
let f = true;
|
||||||
|
|
|
@ -34,7 +34,7 @@ fn main() {
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// 修改 `assert_eq!` 让代码工作
|
// 修改 `assert_eq!` 让代码工作
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = 5;
|
let x = 5;
|
||||||
assert_eq!("u32".to_string(), type_of(&x));
|
assert_eq!("u32".to_string(), type_of(&x));
|
||||||
|
|
|
@ -205,7 +205,7 @@ pub fn add_three(x: i32) -> Option<i32> {
|
||||||
// in lib.rs
|
// in lib.rs
|
||||||
|
|
||||||
mod a {
|
mod a {
|
||||||
/// Add four to the given value and return a [`Option`] type
|
/// Add four to the given value and return a [`Option`] type
|
||||||
/// [`crate::MySpecialFormatter`]
|
/// [`crate::MySpecialFormatter`]
|
||||||
pub fn add_four(x: i32) -> Option<i32> {
|
pub fn add_four(x: i32) -> Option<i32> {
|
||||||
Some(x + 4)
|
Some(x + 4)
|
||||||
|
|
|
@ -333,7 +333,7 @@ fn main() {
|
||||||
当使用泛型参数时,我们往往需要为该参数指定特定的行为,这种指定方式就是通过特征约束来实现的。
|
当使用泛型参数时,我们往往需要为该参数指定特定的行为,这种指定方式就是通过特征约束来实现的。
|
||||||
|
|
||||||
7. 🌟🌟
|
7. 🌟🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(sum(1, 2), 3);
|
assert_eq!(sum(1, 2), 3);
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ fn print_str(s: String) {
|
||||||
```
|
```
|
||||||
|
|
||||||
5. 🌟🌟
|
5. 🌟🌟
|
||||||
```rust, editable
|
```rust,editable
|
||||||
// 不要使用 clone,使用 copy 的方式替代
|
// 不要使用 clone,使用 copy 的方式替代
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = (1, 2, (), "hello".to_string());
|
let x = (1, 2, (), "hello".to_string());
|
||||||
|
|
Loading…
Reference in New Issue