rust-by-practice/solutions/compound-types/slice.md

79 lines
1.2 KiB
Markdown
Raw Normal View History

2022-03-02 07:11:58 -06:00
1.
2022-05-01 00:08:50 -05:00
2022-03-02 07:11:58 -06:00
```rust
fn main() {
let arr = [1, 2, 3];
let s1: &[i32] = &arr[0..2];
let s2: &str = "hello, world";
}
```
2.
2022-05-01 00:08:50 -05:00
2022-03-02 07:11:58 -06:00
```rust
fn main() {
let arr: [char; 3] = ['中', '国', '人'];
let slice = &arr[..2];
2022-05-01 00:08:50 -05:00
2022-03-02 07:11:58 -06:00
// 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.
2022-05-01 00:08:50 -05:00
2022-03-02 07:11:58 -06:00
```rust
fn main() {
let arr: [i32; 5] = [1, 2, 3, 4, 5];
let slice: &[i32] = &arr[1..4];
assert_eq!(slice, &[2, 3, 4]);
}
```
4.
2022-05-01 00:08:50 -05:00
2022-03-02 07:11:58 -06:00
```rust
fn main() {
let s = String::from("hello");
let slice1 = &s[0..2];
let slice2 = &s[..2];
assert_eq!(slice1, slice2);
}
```
5.
2022-05-01 00:08:50 -05:00
2022-03-02 07:11:58 -06:00
```rust
fn main() {
let s = "你好,世界";
let slice = &s[0..3];
assert!(slice == "你");
}
```
6.
2022-05-01 00:08:50 -05:00
2022-03-02 07:11:58 -06:00
```rust
fn main() {
let mut s = String::from("hello world");
// here, &s is `&String` type, but `first_word` need a `&str` type.
// it works because `&String` can be implicitly converted to `&str, If you want know more ,this is called `Deref`
let word = first_word(&s);
println!("the first word is: {}", word);
s.clear();
}
2022-05-01 00:08:50 -05:00
2022-03-02 07:11:58 -06:00
fn first_word(s: &str) -> &str {
&s[..1]
}
```