Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add empty slice example #1538

Merged
merged 2 commits into from
May 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/primitives/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::mem;

// This function borrows a slice
fn analyze_slice(slice: &[i32]) {
println!("first element of the slice: {}", slice[0]);
println!("the slice has {} elements", slice.len());
println!(" first element of the slice: {}", slice[0]);
balroggg marked this conversation as resolved.
Show resolved Hide resolved
println!(" the slice has {} elements", slice.len());
balroggg marked this conversation as resolved.
Show resolved Hide resolved
}

fn main() {
Expand Down Expand Up @@ -48,7 +48,12 @@ fn main() {
println!("borrow a section of the array as a slice");
analyze_slice(&ys[1 .. 4]);

// Example of empty slice `&[]`
let empty_array: [u32; 0] = [];
assert_eq!(&empty_array, &[]);
assert_eq!(&empty_array, &[][..]); // same but more verbose

// Out of bound indexing causes compile error
println!("{}", xs[5]);
//println!("{}", xs[5]);
}
```