mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-14 00:00:02 +03:00
added this as a third generic exercise
This commit is contained in:
parent
bfe7974a5f
commit
c0045bd53d
31
exercises/generics/generics3.rs
Normal file
31
exercises/generics/generics3.rs
Normal 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);
|
||||
}
|
||||
}
|
12
info.toml
12
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]]
|
||||
|
|
Loading…
Reference in a new issue