From 98db5790144a0d32009718e54051b3f7d54ae494 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sat, 8 Jun 2024 23:42:15 +0200 Subject: [PATCH] primitive_types4 solution --- .../04_primitive_types/primitive_types3.rs | 2 +- .../04_primitive_types/primitive_types4.rs | 7 ++---- rustlings-macros/info.toml | 4 ++-- .../04_primitive_types/primitive_types4.rs | 24 ++++++++++++++++++- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/exercises/04_primitive_types/primitive_types3.rs b/exercises/04_primitive_types/primitive_types3.rs index bef5579..9b79c0c 100644 --- a/exercises/04_primitive_types/primitive_types3.rs +++ b/exercises/04_primitive_types/primitive_types3.rs @@ -1,5 +1,5 @@ fn main() { - // TODO: Create an array with at least 100 elements in it where the ??? is. + // TODO: Create an array called `a` with at least 100 elements in it. // let a = ??? if a.len() >= 100 { diff --git a/exercises/04_primitive_types/primitive_types4.rs b/exercises/04_primitive_types/primitive_types4.rs index 661e051..16e4fd9 100644 --- a/exercises/04_primitive_types/primitive_types4.rs +++ b/exercises/04_primitive_types/primitive_types4.rs @@ -1,18 +1,15 @@ -// Get a slice out of Array a where the ??? is so that the test passes. - fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { - use super::*; - #[test] fn slice_out_of_array() { let a = [1, 2, 3, 4, 5]; - let nice_slice = ??? + // TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes. + // let nice_slice = ??? assert_eq!([2, 3, 4], nice_slice); } diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index fc0bee8..313ba6f 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -267,8 +267,8 @@ dir = "04_primitive_types" hint = """ Take a look at the 'Understanding Ownership -> Slices -> Other Slices' section of the book: https://doc.rust-lang.org/book/ch04-03-slices.html and use the -starting and ending (plus one) indices of the items in the `Array` that you -want to end up in the slice. +starting and ending (plus one) indices of the items in the array that you want +to end up in the slice. If you're curious why the first argument of `assert_eq!` does not have an ampersand for a reference since the second argument is a reference, take a look diff --git a/solutions/04_primitive_types/primitive_types4.rs b/solutions/04_primitive_types/primitive_types4.rs index 4e18198..4807e66 100644 --- a/solutions/04_primitive_types/primitive_types4.rs +++ b/solutions/04_primitive_types/primitive_types4.rs @@ -1 +1,23 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + #[test] + fn slice_out_of_array() { + let a = [1, 2, 3, 4, 5]; + // 0 1 2 3 4 <- indices + // ------- + // | + // +--- slice + + // Note that the upper index 4 is excluded. + let nice_slice = &a[1..4]; + assert_eq!([2, 3, 4], nice_slice); + + // The upper index can be included by using the syntax `..=` (with `=` sign) + let nice_slice = &a[1..=3]; + assert_eq!([2, 3, 4], nice_slice); + } +}