From e8d0f132cbeb86f0c1a984d484b5601cf1828d15 Mon Sep 17 00:00:00 2001 From: Vtorygin Valerii Date: Sun, 8 May 2022 06:16:32 +0300 Subject: [PATCH 1/2] Add empty slice example --- src/primitives/array.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/primitives/array.md b/src/primitives/array.md index 50ea52fca4..c30f2cbb53 100644 --- a/src/primitives/array.md +++ b/src/primitives/array.md @@ -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]); + println!(" the slice has {} elements", slice.len()); } fn main() { @@ -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]); } ``` From 374f3759a44c8fe7321f8f7e210a9cfb651f99aa Mon Sep 17 00:00:00 2001 From: Vtorygin Valerii Date: Sun, 8 May 2022 22:15:16 +0300 Subject: [PATCH 2/2] Remove empty spaces --- src/primitives/array.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/primitives/array.md b/src/primitives/array.md index c30f2cbb53..e624f03271 100644 --- a/src/primitives/array.md +++ b/src/primitives/array.md @@ -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]); + println!("the slice has {} elements", slice.len()); } fn main() {