diff --git a/exercises/generics/generics3.rs b/exercises/generics/generics3.rs new file mode 100644 index 00000000..1d529e16 --- /dev/null +++ b/exercises/generics/generics3.rs @@ -0,0 +1,31 @@ +// generics3.rs +// Use your knowledge of generics to fix the `last_on_list` function signature. + +// Execute `rustlings hint generics3` or use the `hint` watch subcommand for a hint. + +// I AM NOT DONE + +fn last_on_list(list: &[&str]) -> &str { + list.last().unwrap() +} + +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn store_str_on_list() { + let names_list = vec!["maria", "jacob", "kacper"]; + let last = last_on_list(&names_list); + } + + #[test] + fn store_numbers_on_list() { + let numbers_list = vec![1, 2, 3]; + let last = last_on_list(&names_list); + } +} \ No newline at end of file diff --git a/info.toml b/info.toml index aacdd4cb..d0f5691e 100644 --- a/info.toml +++ b/info.toml @@ -684,6 +684,18 @@ Maybe we could update the explicit references to this data type somehow? If you are still stuck https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-method-definitions """ +[[exercises]] +name = "generics3" +path = "exercises/generics/generics3.rs" +mode = "test" +hint = """ +Vectors in Rust use generics to create dynamically-sized arrays of any type. +The last_on_list function takes a vector as an argument, but only accepts vectors that store the &str type. +To allow the function to accept vectors that store any type, you can leverage your knowledge about generics. +If you're unsure how to proceed, please refer to the Rust Book at: +https://doc.rust-lang.org/book/ch10-01-syntax.html#in-function-definitions. +""" + # TRAITS [[exercises]]