Change exercise for mutable slices. Closes #26

This commit is contained in:
LukeMathWalker 2024-08-01 15:33:13 +02:00
parent 6029a8fc17
commit f882f0416d
1 changed files with 13 additions and 21 deletions

View File

@ -1,6 +1,5 @@
// TODO: Define a function named `lowercase` that converts all characters in a string to lowercase, // TODO: Define a function named `squared` that raises all `i32`s within a slice to the power of 2.
// modifying the input in place. // The slice should be modified in place.
// Does it need to take a `&mut String`? Does a `&mut str` work? Why or why not?
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -8,29 +7,22 @@ mod tests {
#[test] #[test]
fn empty() { fn empty() {
let mut s = String::from(""); let mut s = vec![];
lowercase(&mut s); squared(&mut s);
assert_eq!(s, ""); assert_eq!(s, vec![]);
} }
#[test] #[test]
fn one_char() { fn one() {
let mut s = String::from("A"); let mut s = [2];
lowercase(&mut s); squared(&mut s);
assert_eq!(s, "a"); assert_eq!(s, [4]);
} }
#[test] #[test]
fn multiple_chars() { fn multiple() {
let mut s = String::from("Hello, World!"); let mut s = vec![2, 4];
lowercase(&mut s); squared(&mut s);
assert_eq!(s, "hello, world!"); assert_eq!(s, vec![4, 16]);
}
#[test]
fn mut_slice() {
let mut s = "Hello, World!".to_string();
lowercase(s.as_mut_str());
assert_eq!(s, "hello, world!");
} }
} }