summaryrefslogtreecommitdiff
path: root/rustlings/solutions
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/solutions')
-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
6 files changed, 78 insertions, 12 deletions
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!");
+ }
}