mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 12:39:42 +00:00
format code in md
This commit is contained in:
@ -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]
|
||||
}
|
||||
|
Reference in New Issue
Block a user