summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
authorliv <mokou@fastmail.com>2023-09-04 14:34:13 +0200
committerGitHub <noreply@github.com>2023-09-04 14:34:13 +0200
commit0aa941873662918aef679bd77b8b7bd6b1a23ec3 (patch)
tree01115c2e72dd0ca51c4bfb2ac1f043d6d9a0a1ca /exercises
parent37cdea9183796909057d8b42ab2866fde1525a40 (diff)
parentabc3013096f20a41009086c0f5ccb1b2b13abf12 (diff)
Merge branch 'main' into chore/update-hints
Diffstat (limited to 'exercises')
-rw-r--r--exercises/conversions/from_into.rs8
-rw-r--r--exercises/error_handling/errors1.rs2
-rw-r--r--exercises/error_handling/errors2.rs6
-rw-r--r--exercises/error_handling/errors4.rs2
-rw-r--r--exercises/if/if1.rs5
-rw-r--r--exercises/if/if3.rs1
-rw-r--r--exercises/move_semantics/move_semantics1.rs15
-rw-r--r--exercises/move_semantics/move_semantics2.rs18
-rw-r--r--exercises/move_semantics/move_semantics3.rs13
-rw-r--r--exercises/move_semantics/move_semantics4.rs16
-rw-r--r--exercises/quiz1.rs2
-rw-r--r--exercises/smart_pointers/cow1.rs2
-rw-r--r--exercises/structs/structs3.rs14
13 files changed, 46 insertions, 58 deletions
diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs
index aba471d..60911f3 100644
--- a/exercises/conversions/from_into.rs
+++ b/exercises/conversions/from_into.rs
@@ -127,14 +127,14 @@ mod tests {
#[test]
fn test_trailing_comma() {
let p: Person = Person::from("Mike,32,");
- assert_eq!(p.name, "John");
- assert_eq!(p.age, 30);
+ assert_eq!(p.name, "Mike");
+ assert_eq!(p.age, 32);
}
#[test]
fn test_trailing_comma_and_some_string() {
let p: Person = Person::from("Mike,32,man");
- assert_eq!(p.name, "John");
- assert_eq!(p.age, 30);
+ assert_eq!(p.name, "Mike");
+ assert_eq!(p.age, 32);
}
}
diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs
index 13d2724..0ba59a5 100644
--- a/exercises/error_handling/errors1.rs
+++ b/exercises/error_handling/errors1.rs
@@ -3,7 +3,7 @@
// This function refuses to generate text to be printed on a nametag if you pass
// it an empty string. It'd be nicer if it explained what the problem was,
// instead of just sometimes returning `None`. Thankfully, Rust has a similar
-// construct to `Result` that can be used to express error conditions. Let's use
+// construct to `Option` that can be used to express error conditions. Let's use
// it!
//
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs
index d86f326..d4a5477 100644
--- a/exercises/error_handling/errors2.rs
+++ b/exercises/error_handling/errors2.rs
@@ -9,9 +9,9 @@
//
// Right now, this function isn't handling the error case at all (and isn't
// handling the success case properly either). What we want to do is: if we call
-// the `parse` function on a string that is not a number, that function will
-// return a `ParseIntError`, and in that case, we want to immediately return
-// that error from our function and not try to multiply and add.
+// the `total_cost` function on a string that is not a number, that function
+// will return a `ParseIntError`, and in that case, we want to immediately
+// return that error from our function and not try to multiply and add.
//
// There are at least two ways to implement this that are both correct-- but one
// is a lot shorter!
diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs
index e04bff7..d6d6fcb 100644
--- a/exercises/error_handling/errors4.rs
+++ b/exercises/error_handling/errors4.rs
@@ -16,7 +16,7 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
- // Hmm...? Why is this only returning an Ok value?
+ // Hmm... Why is this always returning an Ok value?
Ok(PositiveNonzeroInteger(value as u64))
}
}
diff --git a/exercises/if/if1.rs b/exercises/if/if1.rs
index d8108a0..4734d78 100644
--- a/exercises/if/if1.rs
+++ b/exercises/if/if1.rs
@@ -25,4 +25,9 @@ mod tests {
fn fortytwo_is_bigger_than_thirtytwo() {
assert_eq!(42, bigger(32, 42));
}
+
+ #[test]
+ fn equal_numbers() {
+ assert_eq!(42, bigger(42, 42));
+ }
}
diff --git a/exercises/if/if3.rs b/exercises/if/if3.rs
index 73a7025..1696274 100644
--- a/exercises/if/if3.rs
+++ b/exercises/if/if3.rs
@@ -29,6 +29,7 @@ pub fn animal_habitat(animal: &str) -> &'static str {
habitat
}
+// No test changes needed.
#[cfg(test)]
mod tests {
use super::*;
diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs
index 710d20d..e063937 100644
--- a/exercises/move_semantics/move_semantics1.rs
+++ b/exercises/move_semantics/move_semantics1.rs
@@ -5,24 +5,19 @@
// I AM NOT DONE
+#[test]
fn main() {
- let vec0 = Vec::new();
+ let vec0 = vec![22, 44, 66];
let vec1 = fill_vec(vec0);
- println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
-
- vec1.push(88);
-
- println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
- let mut vec = vec;
+ let vec = vec;
- vec.push(22);
- vec.push(44);
- vec.push(66);
+ vec.push(88);
vec
}
diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs
index 72d37fa..baf6bcc 100644
--- a/exercises/move_semantics/move_semantics2.rs
+++ b/exercises/move_semantics/move_semantics2.rs
@@ -1,32 +1,26 @@
// move_semantics2.rs
//
-// Expected output:
-// vec0 has length 3, with contents `[22, 44, 66]`
-// vec1 has length 4, with contents `[22, 44, 66, 88]`
+// Make the test pass by finding a way to keep both Vecs separate!
//
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
// for a hint.
// I AM NOT DONE
+#[test]
fn main() {
- let vec0 = Vec::new();
+ let vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec(vec0);
- println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0);
-
- vec1.push(88);
-
- println!("{} has length {}, with contents `{:?}`", "vec1", vec1.len(), vec1);
+ assert_eq!(vec0, vec![22, 44, 66]);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;
- vec.push(22);
- vec.push(44);
- vec.push(66);
+ vec.push(88);
vec
}
diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs
index ea21493..69e564a 100644
--- a/exercises/move_semantics/move_semantics3.rs
+++ b/exercises/move_semantics/move_semantics3.rs
@@ -8,22 +8,17 @@
// I AM NOT DONE
+#[test]
fn main() {
- let vec0 = Vec::new();
+ let vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec(vec0);
- println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
-
- vec1.push(88);
-
- println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
- vec.push(22);
- vec.push(44);
- vec.push(66);
+ vec.push(88);
vec
}
diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs
index 75a3b6b..80b49db 100644
--- a/exercises/move_semantics/move_semantics4.rs
+++ b/exercises/move_semantics/move_semantics4.rs
@@ -9,25 +9,21 @@
// I AM NOT DONE
+#[test]
fn main() {
- let vec0 = Vec::new();
+ let vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec(vec0);
- println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
-
- vec1.push(88);
-
- println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
}
-// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
+// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
fn fill_vec() -> Vec<i32> {
+ // Instead, let's create and fill the Vec in here - how do you do that?
let mut vec = vec;
- vec.push(22);
- vec.push(44);
- vec.push(66);
+ vec.push(88);
vec
}
diff --git a/exercises/quiz1.rs b/exercises/quiz1.rs
index a9904b8..4ee5ada 100644
--- a/exercises/quiz1.rs
+++ b/exercises/quiz1.rs
@@ -9,7 +9,7 @@
// - An apple costs 2 rustbucks.
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// Write a function that calculates the price of an order of apples given the
-// quantity bought. No hints this time!
+// quantity bought.
//
// No hints this time ;)
diff --git a/exercises/smart_pointers/cow1.rs b/exercises/smart_pointers/cow1.rs
index 7ca9168..5ab5115 100644
--- a/exercises/smart_pointers/cow1.rs
+++ b/exercises/smart_pointers/cow1.rs
@@ -70,7 +70,7 @@ mod tests {
// case the call to `to_mut()` returns a reference to the same data as
// before.
let slice = vec![-1, 0, 1];
- let mut input = Cow::from(slice);
+ let mut input = Cow::from(slice).to_mut();
match abs_all(&mut input) {
// TODO
}
diff --git a/exercises/structs/structs3.rs b/exercises/structs/structs3.rs
index 4851317..7cda5af 100644
--- a/exercises/structs/structs3.rs
+++ b/exercises/structs/structs3.rs
@@ -13,13 +13,15 @@
struct Package {
sender_country: String,
recipient_country: String,
- weight_in_grams: i32,
+ weight_in_grams: u32,
}
impl Package {
- fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
- if weight_in_grams <= 0 {
- panic!("Can not ship a weightless package.")
+ fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Package {
+ if weight_in_grams < 10 {
+ // This is not how you should handle errors in Rust,
+ // but we will learn about error handling later.
+ panic!("Can not ship a package with weight below 10 grams.")
} else {
Package {
sender_country,
@@ -33,7 +35,7 @@ impl Package {
// Something goes here...
}
- fn get_fees(&self, cents_per_gram: i32) -> ??? {
+ fn get_fees(&self, cents_per_gram: u32) -> ??? {
// Something goes here...
}
}
@@ -48,7 +50,7 @@ mod tests {
let sender_country = String::from("Spain");
let recipient_country = String::from("Austria");
- Package::new(sender_country, recipient_country, -2210);
+ Package::new(sender_country, recipient_country, 5);
}
#[test]