mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 12:39:42 +00:00
Fixed mistakes and missing semicolon in array
This commit is contained in:
@ -1,26 +1,26 @@
|
|||||||
# Array
|
# Array
|
||||||
The type of array is `[T; Lengh]`, as you can see, array's lengh is part of their type signature. So their length must be known at compile time.
|
The type of array is `[T; Length]`, as you can see, array's length is part of their type signature. So their length must be known at compile time.
|
||||||
|
|
||||||
For example, you cant initialized an array as below:
|
For example, you cant initialize an array like below:
|
||||||
```rust
|
```rust
|
||||||
fn init_arr(n: i32) {
|
fn init_arr(n: i32) {
|
||||||
let arr = [1; n];
|
let arr = [1; n];
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
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 compiler has no idea of the exact size of the array at compile time.
|
||||||
|
|
||||||
1. 🌟
|
1. 🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// fill the blank with proper array type
|
// Fill the blank with proper array type
|
||||||
let arr: __ = [1, 2, 3, 4, 5];
|
let arr: __ = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
// modify below to make it work
|
// Modify the code below to make it work
|
||||||
assert!(arr.len() == 4);
|
assert!(arr.len() == 4);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -28,16 +28,16 @@ fn main() {
|
|||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// we can ignore parts of the array type or even the whole type, let the compiler infer it for us
|
// 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 arr0 = [1, 2, 3];
|
||||||
let arr: [_; 3] = ['a', 'b', 'c'];
|
let arr: [_; 3] = ['a', 'b', 'c'];
|
||||||
|
|
||||||
// fill the blank
|
// Fill the blank
|
||||||
// Arrays are stack allocated, `std::mem::size_of_val` return the bytes which array occupies
|
// Arrays are stack allocated, `std::mem::size_of_val` returns the bytes which an array occupies
|
||||||
// A char takes 4 byte in Rust: Unicode char
|
// A char takes 4 bytes in Rust: Unicode char
|
||||||
assert!(std::mem::size_of_val(&arr) == __);
|
assert!(std::mem::size_of_val(&arr) == __);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -46,13 +46,13 @@ fn main() {
|
|||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// fill the blank
|
// Fill the blank
|
||||||
let list: [i32; 100] = __ ;
|
let list: [i32; 100] = __ ;
|
||||||
|
|
||||||
assert!(list[0] == 1);
|
assert!(list[0] == 1);
|
||||||
assert!(list.len() == 100);
|
assert!(list.len() == 100);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -60,10 +60,10 @@ fn main() {
|
|||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// fix the error
|
// Fix the error
|
||||||
let _arr = [1, 2, '3'];
|
let _arr = [1, 2, '3'];
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -73,28 +73,28 @@ fn main() {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let arr = ['a', 'b', 'c'];
|
let arr = ['a', 'b', 'c'];
|
||||||
|
|
||||||
let ele = arr[1]; // only modify this line to make the code work!
|
let ele = arr[1]; // Only modify this line to make the code work!
|
||||||
|
|
||||||
assert!(ele == 'a');
|
assert!(ele == 'a');
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
6. 🌟 Out of bounds indexing causes `panic`.
|
6. 🌟 Out of bounds indexing causes `panic`.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix the error
|
// Fix the error
|
||||||
fn main() {
|
fn main() {
|
||||||
let names = [String::from("Sunfei"), "Sunface".to_string()];
|
let names = [String::from("Sunfei"), "Sunface".to_string()];
|
||||||
|
|
||||||
// `get` returns an Option<T>, it's safe to use
|
// `Get` returns an Option<T>, it's safe to use
|
||||||
let name0 = names.get(0).unwrap();
|
let name0 = names.get(0).unwrap();
|
||||||
|
|
||||||
// but indexing is not safe
|
// But indexing is not safe
|
||||||
let _name1 = &names[2];
|
let _name1 = &names[2];
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user