added this as a third generic exercise

This commit is contained in:
Kacper Poneta 2024-07-11 21:47:30 +02:00
parent bfe7974a5f
commit c0045bd53d
2 changed files with 43 additions and 0 deletions

View file

@ -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);
}
}

View file

@ -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 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 # TRAITS
[[exercises]] [[exercises]]