summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rustlings/exercises/04_primitive_types/primitive_types1.rs4
-rw-r--r--rustlings/exercises/04_primitive_types/primitive_types2.rs7
-rw-r--r--rustlings/exercises/04_primitive_types/primitive_types3.rs3
-rw-r--r--rustlings/exercises/04_primitive_types/primitive_types4.rs3
-rw-r--r--rustlings/exercises/04_primitive_types/primitive_types5.rs3
-rw-r--r--rustlings/exercises/04_primitive_types/primitive_types6.rs4
-rw-r--r--rustlings/solutions/04_primitive_types/primitive_types1.rs11
-rw-r--r--rustlings/solutions/04_primitive_types/primitive_types2.rs21
-rw-r--r--rustlings/solutions/04_primitive_types/primitive_types3.rs11
-rw-r--r--rustlings/solutions/04_primitive_types/primitive_types4.rs23
-rw-r--r--rustlings/solutions/04_primitive_types/primitive_types5.rs8
-rw-r--r--rustlings/solutions/04_primitive_types/primitive_types6.rs16
12 files changed, 84 insertions, 30 deletions
diff --git a/rustlings/exercises/04_primitive_types/primitive_types1.rs b/rustlings/exercises/04_primitive_types/primitive_types1.rs
index 84923c7..778c0f6 100644
--- a/rustlings/exercises/04_primitive_types/primitive_types1.rs
+++ b/rustlings/exercises/04_primitive_types/primitive_types1.rs
@@ -6,9 +6,7 @@ fn main() {
println!("Good morning!");
}
- // TODO: Define a boolean variable with the name `is_evening` before the `if` statement below.
- // The value of the variable should be the negation (opposite) of `is_morning`.
- // let …
+ let is_evening = !is_morning;
if is_evening {
println!("Good evening!");
}
diff --git a/rustlings/exercises/04_primitive_types/primitive_types2.rs b/rustlings/exercises/04_primitive_types/primitive_types2.rs
index 1401847..a45f84d 100644
--- a/rustlings/exercises/04_primitive_types/primitive_types2.rs
+++ b/rustlings/exercises/04_primitive_types/primitive_types2.rs
@@ -12,12 +12,7 @@ fn main() {
println!("Neither alphabetic nor numeric!");
}
- // TODO: Analogous to the example before, declare a variable called `your_character`
- // below with your favorite character.
- // Try a letter, try a digit (in single quotes), try a special character, try a character
- // from a different language than your own, try an emoji 😉
- // let your_character = '';
-
+ let your_character = '😉';
if your_character.is_alphabetic() {
println!("Alphabetical!");
} else if your_character.is_numeric() {
diff --git a/rustlings/exercises/04_primitive_types/primitive_types3.rs b/rustlings/exercises/04_primitive_types/primitive_types3.rs
index 9b79c0c..c5a259d 100644
--- a/rustlings/exercises/04_primitive_types/primitive_types3.rs
+++ b/rustlings/exercises/04_primitive_types/primitive_types3.rs
@@ -1,6 +1,5 @@
fn main() {
- // TODO: Create an array called `a` with at least 100 elements in it.
- // let a = ???
+ let a = [0; 1000];
if a.len() >= 100 {
println!("Wow, that's a big array!");
diff --git a/rustlings/exercises/04_primitive_types/primitive_types4.rs b/rustlings/exercises/04_primitive_types/primitive_types4.rs
index 16e4fd9..81af3e2 100644
--- a/rustlings/exercises/04_primitive_types/primitive_types4.rs
+++ b/rustlings/exercises/04_primitive_types/primitive_types4.rs
@@ -8,8 +8,7 @@ mod tests {
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];
- // TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes.
- // let nice_slice = ???
+ let nice_slice = &a[1..4];
assert_eq!([2, 3, 4], nice_slice);
}
diff --git a/rustlings/exercises/04_primitive_types/primitive_types5.rs b/rustlings/exercises/04_primitive_types/primitive_types5.rs
index 6e00ef5..a3e60d8 100644
--- a/rustlings/exercises/04_primitive_types/primitive_types5.rs
+++ b/rustlings/exercises/04_primitive_types/primitive_types5.rs
@@ -1,8 +1,7 @@
fn main() {
let cat = ("Furry McFurson", 3.5);
- // TODO: Destructure the `cat` tuple in one statement so that the println works.
- // let /* your pattern here */ = cat;
+ let (name, age) = cat;
println!("{name} is {age} years old");
}
diff --git a/rustlings/exercises/04_primitive_types/primitive_types6.rs b/rustlings/exercises/04_primitive_types/primitive_types6.rs
index a97e531..dec639a 100644
--- a/rustlings/exercises/04_primitive_types/primitive_types6.rs
+++ b/rustlings/exercises/04_primitive_types/primitive_types6.rs
@@ -8,9 +8,7 @@ mod tests {
fn indexing_tuple() {
let numbers = (1, 2, 3);
- // TODO: Use a tuple index to access the second element of `numbers`
- // and assign it to a variable called `second`.
- // let second = ???;
+ let second = numbers.1;
assert_eq!(second, 2, "This is not the 2nd number in the tuple!");
}
diff --git a/rustlings/solutions/04_primitive_types/primitive_types1.rs b/rustlings/solutions/04_primitive_types/primitive_types1.rs
index dcf2377..fac6ec0 100644
--- a/rustlings/solutions/04_primitive_types/primitive_types1.rs
+++ b/rustlings/solutions/04_primitive_types/primitive_types1.rs
@@ -1,4 +1,11 @@
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ let is_morning = true;
+ if is_morning {
+ println!("Good morning!");
+ }
+
+ let is_evening = !is_morning;
+ if is_evening {
+ println!("Good evening!");
+ }
}
diff --git a/rustlings/solutions/04_primitive_types/primitive_types2.rs b/rustlings/solutions/04_primitive_types/primitive_types2.rs
index dcf2377..eecc680 100644
--- a/rustlings/solutions/04_primitive_types/primitive_types2.rs
+++ b/rustlings/solutions/04_primitive_types/primitive_types2.rs
@@ -1,4 +1,21 @@
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ let my_first_initial = 'C';
+ if my_first_initial.is_alphabetic() {
+ println!("Alphabetical!");
+ } else if my_first_initial.is_numeric() {
+ println!("Numerical!");
+ } else {
+ println!("Neither alphabetic nor numeric!");
+ }
+
+ // Example with an emoji.
+ let your_character = '🦀';
+
+ if your_character.is_alphabetic() {
+ println!("Alphabetical!");
+ } else if your_character.is_numeric() {
+ println!("Numerical!");
+ } else {
+ println!("Neither alphabetic nor numeric!");
+ }
}
diff --git a/rustlings/solutions/04_primitive_types/primitive_types3.rs b/rustlings/solutions/04_primitive_types/primitive_types3.rs
index dcf2377..8dd109f 100644
--- a/rustlings/solutions/04_primitive_types/primitive_types3.rs
+++ b/rustlings/solutions/04_primitive_types/primitive_types3.rs
@@ -1,4 +1,11 @@
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // An array with 100 elements of the value 42.
+ let a = [42; 100];
+
+ if a.len() >= 100 {
+ println!("Wow, that's a big array!");
+ } else {
+ println!("Meh, I eat arrays like that for breakfast.");
+ panic!("Array not big enough, more elements needed");
+ }
}
diff --git a/rustlings/solutions/04_primitive_types/primitive_types4.rs b/rustlings/solutions/04_primitive_types/primitive_types4.rs
index dcf2377..4807e66 100644
--- a/rustlings/solutions/04_primitive_types/primitive_types4.rs
+++ b/rustlings/solutions/04_primitive_types/primitive_types4.rs
@@ -1,4 +1,23 @@
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // 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);
+ }
}
diff --git a/rustlings/solutions/04_primitive_types/primitive_types5.rs b/rustlings/solutions/04_primitive_types/primitive_types5.rs
index dcf2377..46d7ae8 100644
--- a/rustlings/solutions/04_primitive_types/primitive_types5.rs
+++ b/rustlings/solutions/04_primitive_types/primitive_types5.rs
@@ -1,4 +1,8 @@
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ let cat = ("Furry McFurson", 3.5);
+
+ // Destructuring the tuple.
+ let (name, age) = cat;
+
+ println!("{name} is {age} years old");
}
diff --git a/rustlings/solutions/04_primitive_types/primitive_types6.rs b/rustlings/solutions/04_primitive_types/primitive_types6.rs
index dcf2377..9b7c277 100644
--- a/rustlings/solutions/04_primitive_types/primitive_types6.rs
+++ b/rustlings/solutions/04_primitive_types/primitive_types6.rs
@@ -1,4 +1,16 @@
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn indexing_tuple() {
+ let numbers = (1, 2, 3);
+
+ // Tuple indexing syntax.
+ let second = numbers.1;
+
+ assert_eq!(second, 2, "This is not the 2nd number in the tuple!");
+ }
}