mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 12:39:42 +00:00
add solutions for array.md
This commit is contained in:
@ -0,0 +1,63 @@
|
|||||||
|
1.
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let arr: [i32; 5] = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
|
assert!(arr.len() == 5);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
3.
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let list: [i32; 100] = [1; 100];
|
||||||
|
|
||||||
|
assert!(list[0] == 1);
|
||||||
|
assert!(list.len() == 100);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
4.
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
// fix the error
|
||||||
|
let _arr = [1, 2, 3];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
5.
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let arr = ['a', 'b', 'c'];
|
||||||
|
|
||||||
|
let ele = arr[0];
|
||||||
|
|
||||||
|
assert!(ele == 'a');
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
// but indexing is not safe
|
||||||
|
let _name1 = &names[1];
|
||||||
|
}
|
||||||
|
```
|
@ -10,7 +10,7 @@ fn init_arr(n: i32) {
|
|||||||
|
|
||||||
This will cause an error, because the compile have no idea of the exact size of the array in compile time.
|
This will cause an error, because the compile have no idea of the exact size of the array in compile time.
|
||||||
|
|
||||||
🌟
|
1. 🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -22,7 +22,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟
|
2. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -37,7 +37,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟 All elements in an array can be initialized to the same value at once.
|
3. 🌟 All elements in an array can be initialized to the same value at once.
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟 All elements in an array must be of the same type
|
4. 🌟 All elements in an array must be of the same type
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -59,7 +59,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟 Indexing starts at 0.
|
5. 🌟 Indexing starts at 0.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -71,7 +71,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟 Out of bounds indexing causes `panic`.
|
6. 🌟 Out of bounds indexing causes `panic`.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix the error
|
// fix the error
|
||||||
|
Reference in New Issue
Block a user