Skip to content

Commit 98db579

Browse files
committed
primitive_types4 solution
1 parent 0338b1c commit 98db579

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

exercises/04_primitive_types/primitive_types3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
2-
// TODO: Create an array with at least 100 elements in it where the ??? is.
2+
// TODO: Create an array called `a` with at least 100 elements in it.
33
// let a = ???
44

55
if a.len() >= 100 {

exercises/04_primitive_types/primitive_types4.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
// Get a slice out of Array a where the ??? is so that the test passes.
2-
31
fn main() {
42
// You can optionally experiment here.
53
}
64

75
#[cfg(test)]
86
mod tests {
9-
use super::*;
10-
117
#[test]
128
fn slice_out_of_array() {
139
let a = [1, 2, 3, 4, 5];
1410

15-
let nice_slice = ???
11+
// TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes.
12+
// let nice_slice = ???
1613

1714
assert_eq!([2, 3, 4], nice_slice);
1815
}

rustlings-macros/info.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ dir = "04_primitive_types"
267267
hint = """
268268
Take a look at the 'Understanding Ownership -> Slices -> Other Slices' section
269269
of the book: https://doc.rust-lang.org/book/ch04-03-slices.html and use the
270-
starting and ending (plus one) indices of the items in the `Array` that you
271-
want to end up in the slice.
270+
starting and ending (plus one) indices of the items in the array that you want
271+
to end up in the slice.
272272
273273
If you're curious why the first argument of `assert_eq!` does not have an
274274
ampersand for a reference since the second argument is a reference, take a look
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1-
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
1+
fn main() {
2+
// You can optionally experiment here.
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
#[test]
8+
fn slice_out_of_array() {
9+
let a = [1, 2, 3, 4, 5];
10+
// 0 1 2 3 4 <- indices
11+
// -------
12+
// |
13+
// +--- slice
14+
15+
// Note that the upper index 4 is excluded.
16+
let nice_slice = &a[1..4];
17+
assert_eq!([2, 3, 4], nice_slice);
18+
19+
// The upper index can be included by using the syntax `..=` (with `=` sign)
20+
let nice_slice = &a[1..=3];
21+
assert_eq!([2, 3, 4], nice_slice);
22+
}
23+
}

0 commit comments

Comments
 (0)