add solutions for slice.md
This commit is contained in:
parent
7000b47b50
commit
d4bfe87373
|
@ -0,0 +1,72 @@
|
|||
1.
|
||||
```rust
|
||||
fn main() {
|
||||
let arr = [1, 2, 3];
|
||||
let s1: &[i32] = &arr[0..2];
|
||||
|
||||
let s2: &str = "hello, world";
|
||||
}
|
||||
```
|
||||
|
||||
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];
|
||||
let slice: &[i32] = &arr[1..4];
|
||||
assert_eq!(slice, &[2, 3, 4]);
|
||||
}
|
||||
```
|
||||
|
||||
4.
|
||||
```rust
|
||||
fn main() {
|
||||
let s = String::from("hello");
|
||||
|
||||
let slice1 = &s[0..2];
|
||||
let slice2 = &s[..2];
|
||||
|
||||
assert_eq!(slice1, slice2);
|
||||
}
|
||||
```
|
||||
|
||||
5.
|
||||
```rust
|
||||
fn main() {
|
||||
let s = "你好,世界";
|
||||
let slice = &s[0..3];
|
||||
|
||||
assert!(slice == "你");
|
||||
}
|
||||
```
|
||||
|
||||
6.
|
||||
```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();
|
||||
|
||||
}
|
||||
fn first_word(s: &str) -> &str {
|
||||
&s[..1]
|
||||
}
|
||||
```
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
# Slice
|
||||
Slices are similar to arrays, but their length is not known at compile time, so you can't use slice directly.
|
||||
|
||||
🌟🌟 Here, both `[i32]` and `str` are slice types, but directly using it will cause errors. You have to use the reference of the slice instead: `&[i32]`, `&str`.
|
||||
1. 🌟🌟 Here, both `[i32]` and `str` are slice types, but directly using it will cause errors. You have to use the reference of the slice instead: `&[i32]`, `&str`.
|
||||
```rust,editable
|
||||
|
||||
// fix the errors, DON'T add new lines!
|
||||
|
@ -15,7 +15,7 @@ fn main() {
|
|||
|
||||
A slice reference is a two-word object, for simplicity reasons, from now on we will use slice instead of `slice reference`. The first word is a pointer to the data, and the second word is the length of the slice. The word size is the same as usize, determined by the processor architecture eg 64 bits on an x86-64. Slices can be used to borrow a section of an array, and have the type signature `&[T]`.
|
||||
|
||||
🌟🌟🌟
|
||||
2. 🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
@ -29,7 +29,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟
|
||||
3. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
@ -41,21 +41,21 @@ fn main() {
|
|||
```
|
||||
|
||||
### string slices
|
||||
🌟
|
||||
4. 🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let s = String::from("hello");
|
||||
|
||||
let slice1 = &s[0..2];
|
||||
// fill the blank to make the code work
|
||||
// fill the blank to make the code work, DON'T USE 0..2 again
|
||||
let slice2 = &s[__];
|
||||
|
||||
assert_eq!(slice1, slice2);
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
5. 🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
@ -67,7 +67,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟 `&String` can be implicitly converted into `&str`.
|
||||
6. 🌟🌟 `&String` can be implicitly converted into `&str`.
|
||||
```rust,editable
|
||||
|
||||
// fix errors
|
||||
|
@ -75,7 +75,7 @@ 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` implicitly be converted to `&str, If you want know more ,this is called `Deref`
|
||||
// it works because `&String` can be implicitly converted to `&str, If you want know more ,this is called `Deref`
|
||||
let word = first_word(&s);
|
||||
|
||||
s.clear(); // error!
|
||||
|
|
|
@ -50,7 +50,7 @@ fn main() {
|
|||
let s = String::from("hello");
|
||||
|
||||
let slice1 = &s[0..2];
|
||||
// 填空
|
||||
// 填空,不要再使用 0..2
|
||||
let slice2 = &s[__];
|
||||
|
||||
assert_eq!(slice1, slice2);
|
||||
|
|
Loading…
Reference in New Issue