format code in md

This commit is contained in:
likzn
2022-05-01 13:08:50 +08:00
parent dad4052485
commit e09080f88a
43 changed files with 405 additions and 124 deletions

View File

@@ -1,4 +1,5 @@
1.
```rust
fn main() {
let arr: [i32; 5] = [1, 2, 3, 4, 5];
@@ -8,12 +9,13 @@ fn main() {
```
2.
```rust
fn main() {
// we can ignore parts of the array type or even the whole type, let the compiler infer it for us
let arr0 = [1, 2, 3];
let arr: [_; 3] = ['a', 'b', 'c'];
// Arrays are stack allocated, `std::mem::size_of_val` return the bytes which array occupies
// A char takes 4 byte in Rust: Unicode char
assert!(std::mem::size_of_val(&arr) == 12);
@@ -21,6 +23,7 @@ fn main() {
```
3.
```rust
fn main() {
let list: [i32; 100] = [1; 100];
@@ -31,6 +34,7 @@ fn main() {
```
4.
```rust
fn main() {
// fix the error
@@ -39,10 +43,11 @@ fn main() {
```
5.
```rust
fn main() {
let arr = ['a', 'b', 'c'];
let ele = arr[0];
assert!(ele == 'a');
@@ -50,10 +55,11 @@ fn main() {
```
6.
```rust
fn main() {
let names = [String::from("Sunfei"), "Sunface".to_string()];
// `get` returns an Option<T>, it's safe to use
let name0 = names.get(0).unwrap();

View File

@@ -1,4 +1,5 @@
1.
```rust
enum Number {
Zero,
@@ -28,6 +29,7 @@ fn main() {
```
2.
```rust
enum Message {
Quit,
@@ -43,6 +45,7 @@ fn main() {
```
3.
```rust
enum Message {
Quit,
@@ -63,6 +66,7 @@ fn main() {
```
4.
```rust
#[derive(Debug)]
enum Message {
@@ -75,14 +79,14 @@ enum Message {
fn main() {
let msgs: [Message; 3] = [
Message::Quit,
Message::Move{x:1, y:3},
Message::ChangeColor(255,255,0)
Message::Move { x: 1, y: 3 },
Message::ChangeColor(255, 255, 0)
];
for msg in msgs {
show_message(msg)
}
}
}
fn show_message(msg: Message) {
println!("{:?}", msg);
@@ -90,6 +94,7 @@ fn show_message(msg: Message) {
```
5.
```rust
fn main() {
let five = Some(5);
@@ -113,6 +118,7 @@ fn plus_one(x: Option<i32>) -> Option<i32> {
```
6.
```rust
use crate::List::*;
@@ -162,10 +168,10 @@ impl List {
// `format!` is similar to `print!`, but returns a heap
// allocated string instead of printing to the console
format!("{}, {}", head, tail.stringify())
},
}
Nil => {
format!("Nil")
},
}
}
}
}

View File

@@ -1,4 +1,5 @@
1.
```rust
fn main() {
let arr = [1, 2, 3];
@@ -9,18 +10,20 @@ fn main() {
```
2.
```rust
fn main() {
let arr: [char; 3] = ['中', '国', '人'];
let slice = &arr[..2];
// TIPS: slice( reference ) IS NOT an array, if it is an array, then `assert!` will passed: each of the two UTF-8 chars '中' and '国' occupies 3 bytes, 2 * 3 = 6
assert!(std::mem::size_of_val(&slice) == 16);
}
```
3.
```rust
fn main() {
let arr: [i32; 5] = [1, 2, 3, 4, 5];
@@ -30,6 +33,7 @@ fn main() {
```
4.
```rust
fn main() {
let s = String::from("hello");
@@ -42,6 +46,7 @@ fn main() {
```
5.
```rust
fn main() {
let s = "你好,世界";
@@ -52,6 +57,7 @@ fn main() {
```
6.
```rust
fn main() {
let mut s = String::from("hello world");
@@ -63,8 +69,8 @@ fn main() {
println!("the first word is: {}", word);
s.clear();
}
fn first_word(s: &str) -> &str {
&s[..1]
}

View File

@@ -1,11 +1,13 @@
1.
1.
```rust
fn main() {
let s: &str = "hello, world";
}
```
2.
2.
```rust
fn main() {
let s: Box<str> = "hello, world".into();
@@ -16,6 +18,7 @@ fn main() {
println!("{}",s)
}
```
```rust
fn main() {
let s: Box<&str> = "hello, world".into();
@@ -27,7 +30,8 @@ fn greetings(s: &str) {
}
```
3.
3.
```rust
fn main() {
let mut s = String::new();
@@ -38,7 +42,8 @@ fn main() {
}
```
4.
4.
```rust
fn main() {
let mut s = String::from("hello");
@@ -50,7 +55,8 @@ fn main() {
}
```
5.
5.
```rust
fn main() {
let s = String::from("I like dogs");
@@ -61,7 +67,8 @@ fn main() {
}
```
6.
6.
```rust
fn main() {
let s1 = String::from("hello,");
@@ -72,7 +79,8 @@ fn main() {
}
```
7.
7.
```rust
fn main() {
let s = "hello, world".to_string();
@@ -95,10 +103,11 @@ fn greetings(s: String) {
}
```
8.
8.
```rust
fn main() {
let s = "hello, world".to_string();
let s = "hello, world".to_string();
let s1: &str = &s;
}
```
@@ -112,12 +121,13 @@ fn main() {
```rust
fn main() {
let s = "hello, world".to_string();
let s = "hello, world".to_string();
let s1: String = s;
}
```
9.
9.
```rust
fn main() {
// You can use escapes to write bytes by their hexadecimal values
@@ -140,7 +150,8 @@ fn main() {
}
```
10.
10.
```rust
fn main() {
let raw_str = "Escapes don't work here: \x3F \u{211D}";
@@ -162,7 +173,8 @@ fn main() {
}
```
11.
11.
```rust
fn main() {
let s1 = String::from("hi,中国");
@@ -174,7 +186,8 @@ fn main() {
}
```
12.
12.
```rust
fn main() {
for c in "你好,世界".chars() {

View File

@@ -1,4 +1,5 @@
1.
```rust
struct Person {
name: String,
@@ -16,6 +17,7 @@ fn main() {
```
2.
```rust
struct Unit;
trait SomeTrait {
@@ -35,6 +37,7 @@ fn do_something_with_unit(u: Unit) { }
```
3.
```rust
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
@@ -52,6 +55,7 @@ fn check_color(p: Point) {
```
4.
```rust
struct Person {
name: String,
@@ -72,6 +76,7 @@ fn main() {
```
5.
```rust
struct Person {
name: String,
@@ -88,6 +93,7 @@ fn build_person(name: String, age: u8) -> Person {
```
6.
```rust
struct User {
active: bool,
@@ -115,6 +121,7 @@ fn set_email(u: User) -> User {
```
7.
```rust
#[derive(Debug)]
struct Rectangle {
@@ -136,6 +143,7 @@ fn main() {
```
8.
```rust
#[derive(Debug)]
struct File {

View File

@@ -1,4 +1,5 @@
1.
```rust
fn main() {
let _t0: (u8,i16) = (0, -1);
@@ -9,6 +10,7 @@ fn main() {
```
2.
```rust
fn main() {
let t = ("i", "am", "sunface");
@@ -17,6 +19,7 @@ fn main() {
```
3.
```rust
fn main() {
let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
@@ -25,6 +28,7 @@ fn main() {
```
4.
```rust
fn main() {
let tup = (1, 6.4, "hello");
@@ -38,6 +42,7 @@ fn main() {
```
5.
```rust
fn main() {
let (x, y, z);
@@ -52,6 +57,7 @@ fn main() {
```
6.
```rust
fn main() {
let (x, y) = sum_multiply((2, 3));