mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
format code in md
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
1.
|
||||
|
||||
```rust
|
||||
// FILL in the blanks and FIX the erros
|
||||
use std::collections::HashMap;
|
||||
@ -29,6 +30,7 @@ fn main() {
|
||||
```
|
||||
|
||||
2.
|
||||
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
fn main() {
|
||||
@ -73,9 +75,11 @@ fn main() {
|
||||
```
|
||||
|
||||
3.
|
||||
|
||||
```rust
|
||||
// FILL in the blanks
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() {
|
||||
// type inference lets us omit an explicit type signature (which
|
||||
// would be `HashMap<&str, u8>` in this example).
|
||||
@ -109,6 +113,7 @@ fn random_stat_buff() -> u8 {
|
||||
```
|
||||
|
||||
4.
|
||||
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -144,19 +149,21 @@ fn main() {
|
||||
```
|
||||
|
||||
5.
|
||||
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
fn main() {
|
||||
let v1 = 10;
|
||||
let mut m1 = HashMap::new();
|
||||
m1.insert(v1, v1);
|
||||
println!("v1 is still usable after inserting to hashmap : {}", v1);
|
||||
|
||||
// &str implements Copy trait
|
||||
let v2 = "hello";
|
||||
let mut m2 = HashMap::new();
|
||||
m2.insert(v2, v1);
|
||||
|
||||
assert_eq!(v2, "hello");
|
||||
fn main() {
|
||||
let v1 = 10;
|
||||
let mut m1 = HashMap::new();
|
||||
m1.insert(v1, v1);
|
||||
println!("v1 is still usable after inserting to hashmap : {}", v1);
|
||||
|
||||
// &str implements Copy trait
|
||||
let v2 = "hello";
|
||||
let mut m2 = HashMap::new();
|
||||
m2.insert(v2, v1);
|
||||
|
||||
assert_eq!(v2, "hello");
|
||||
}
|
||||
```
|
@ -1,4 +1,5 @@
|
||||
1.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s: String = String::from("hello, ");
|
||||
@ -36,6 +37,7 @@ fn borrow_string(s: &str) {
|
||||
```
|
||||
|
||||
2.
|
||||
|
||||
```rust
|
||||
// FILL in the blanks
|
||||
fn main() {
|
||||
@ -82,6 +84,7 @@ fn main() {
|
||||
|
||||
|
||||
4.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let s = String::from("hello, 世界");
|
||||
@ -102,6 +105,7 @@ fn main() {
|
||||
```
|
||||
|
||||
5.
|
||||
|
||||
```rust
|
||||
// FILL in the blanks
|
||||
fn main() {
|
||||
@ -123,6 +127,7 @@ fn main() {
|
||||
```
|
||||
|
||||
6.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s = String::with_capacity(25);
|
||||
@ -139,6 +144,7 @@ fn main() {
|
||||
```
|
||||
|
||||
7.
|
||||
|
||||
```rust
|
||||
use std::mem;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
1.
|
||||
1.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let arr: [u8; 3] = [1, 2, 3];
|
||||
@ -30,6 +31,7 @@ fn is_vec(v: &Vec<u8>) {}
|
||||
```
|
||||
|
||||
2.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut v1 = Vec::from([1, 2, 4]);
|
||||
@ -45,7 +47,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
3.
|
||||
3.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
// array -> Vec
|
||||
@ -72,7 +75,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
4.
|
||||
4.
|
||||
|
||||
```rust,editable
|
||||
fn main() {
|
||||
let mut v = Vec::from([1, 2, 3]);
|
||||
@ -94,7 +98,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
5.
|
||||
5.
|
||||
|
||||
```rust
|
||||
// FIX the errors
|
||||
fn main() {
|
||||
@ -119,7 +124,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
6.
|
||||
6.
|
||||
|
||||
```rust
|
||||
// FIX the errors
|
||||
fn main() {
|
||||
@ -155,20 +161,22 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
7.
|
||||
7.
|
||||
|
||||
```rust
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum IpAddr {
|
||||
V4(String),
|
||||
V6(String),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// FILL in the blank
|
||||
let v : Vec<IpAddr>= vec![
|
||||
let v: Vec<IpAddr> = vec![
|
||||
IpAddr::V4("127.0.0.1".to_string()),
|
||||
IpAddr::V6("::1".to_string())
|
||||
];
|
||||
|
||||
|
||||
// Comparing two enums need to derive the PartialEq trait
|
||||
assert_eq!(v[0], IpAddr::V4("127.0.0.1".to_string()));
|
||||
assert_eq!(v[1], IpAddr::V6("::1".to_string()));
|
||||
@ -177,7 +185,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
8.
|
||||
8.
|
||||
|
||||
```rust
|
||||
trait IpAddr {
|
||||
fn display(&self);
|
||||
|
Reference in New Issue
Block a user