mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
added exercise
This commit is contained in:
parent
0847b3a4bf
commit
55cc8584bd
35
exercises/14_generics/generics3.rs
Normal file
35
exercises/14_generics/generics3.rs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
// generics3.rs
|
||||||
|
// Execute `rustlings hint generics3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
|
// TODO Use your knowledge of generics to enchance the `stringify` function by only changing the signature.
|
||||||
|
fn stringify(list: &[&str]) -> String {
|
||||||
|
let items_str = list
|
||||||
|
.iter()
|
||||||
|
.map(|e| e.to_string())
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(", ");
|
||||||
|
format!("[{items_str}]")
|
||||||
|
}
|
||||||
|
|
||||||
|
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 stringified = stringify(&names_list);
|
||||||
|
assert_eq!(stringified, "[maria, jacob, kacper]".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_numbers_on_list() {
|
||||||
|
let numbers_list = vec![1, 2, 3];
|
||||||
|
let stringified = stringify(&numbers_list);
|
||||||
|
assert_eq!(stringified, "[1, 2, 3]".to_string());
|
||||||
|
}
|
||||||
|
}
|
|
@ -741,6 +741,20 @@ hint = """
|
||||||
Related section in The Book:
|
Related section in The Book:
|
||||||
https://doc.rust-lang.org/book/ch10-01-syntax.html#in-method-definitions"""
|
https://doc.rust-lang.org/book/ch10-01-syntax.html#in-method-definitions"""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "generics3"
|
||||||
|
dir = "14_generics"
|
||||||
|
hint = """
|
||||||
|
Vectors in Rust use generics to create dynamically-sized arrays of any type.
|
||||||
|
The stringify 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.
|
||||||
|
|
||||||
|
*Please note that in `stringify` we want to use `.to_string()` on vector elements
|
||||||
|
Refer to this: https://doc.rust-lang.org/book/ch10-02-traits.html
|
||||||
|
"""
|
||||||
|
|
||||||
# TRAITS
|
# TRAITS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
|
38
solutions/14_generics/generics3.rs
Normal file
38
solutions/14_generics/generics3.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
// generics3.rs
|
||||||
|
// Execute `rustlings hint generics3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
|
// Here we add generic in function declaration so function can work with different types
|
||||||
|
fn stringify<T>(list: &Vec<T>) -> String
|
||||||
|
where
|
||||||
|
T: ToString // here we also specify that T needs to implement ToString trait so we can use .to_string() on the vetor elements
|
||||||
|
{
|
||||||
|
let items_str = list
|
||||||
|
.iter()
|
||||||
|
.map(|e| e.to_string())
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(", ");
|
||||||
|
format!("[{items_str}]")
|
||||||
|
}
|
||||||
|
|
||||||
|
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 stringified = stringify(&names_list);
|
||||||
|
assert_eq!(stringified, "[maria, jacob, kacper]".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_numbers_on_list() {
|
||||||
|
let numbers_list = vec![1, 2, 3];
|
||||||
|
let stringified = stringify(&numbers_list);
|
||||||
|
assert_eq!(stringified, "[1, 2, 3]".to_string());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue