From bfe7974a5fa82a6e74ac7bdb309aaf4819201d51 Mon Sep 17 00:00:00 2001 From: Kacper Poneta Date: Wed, 5 Apr 2023 22:10:13 +0200 Subject: [PATCH] updated task --- exercises/generics/generics1.rs | 18 +++++++++++++----- info.toml | 7 +++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs index 4c34ae47..35fdc9ef 100644 --- a/exercises/generics/generics1.rs +++ b/exercises/generics/generics1.rs @@ -1,11 +1,19 @@ -// This shopping list program isn't compiling! -// Use your knowledge of generics to fix it. +// generics1.rs +// Use your knowledge of generics to fix the function signature. // Execute `rustlings hint generics1` or use the `hint` watch subcommand for a hint. // I AM NOT DONE -fn main() { - let mut shopping_list: Vec = Vec::new(); - shopping_list.push("milk"); +fn last_on_list(list: &[&str]) -> &str { + list.last().unwrap() +} + +// Do not change the main method +fn main() { + let names_list = vec!["maria", "jacob", "kacper"]; + println!("last name on the list is: {}", last_on_list(&names_list)); + + let numbers_list = vec![1, 2, 3]; + println!("last number on the list is: {}", last_on_list(&numbers_list)); } diff --git a/info.toml b/info.toml index ef891435..aacdd4cb 100644 --- a/info.toml +++ b/info.toml @@ -667,8 +667,11 @@ name = "generics1" path = "exercises/generics/generics1.rs" mode = "compile" hint = """ -Vectors in Rust make use of generics to create dynamically sized arrays of any type. -You need to tell the compiler what type we are pushing onto this vector.""" +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.""" [[exercises]] name = "generics2"