mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-27 00:00:03 +03:00
changed the task to make it more appropriate
This commit is contained in:
parent
938b90e5f2
commit
52af0674c1
|
@ -1,14 +1,13 @@
|
||||||
// generics3.rs
|
// generics3.rs
|
||||||
// Execute `rustlings hint generics3` or use the `hint` watch subcommand for a hint.
|
// 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.
|
// This function should take an array of `Option` elements and returns array of not None elements
|
||||||
fn stringify(list: &[&str]) -> String {
|
// TODO fix this function signature
|
||||||
let items_str = list
|
fn into_dispose_nulls(list: Vec<Option<&str>>) -> Vec<&str> {
|
||||||
.iter()
|
list
|
||||||
.map(|e| e.to_string())
|
.into_iter()
|
||||||
.collect::<Vec<String>>()
|
.flatten()
|
||||||
.join(", ");
|
.collect()
|
||||||
format!("[{items_str}]")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -21,15 +20,35 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn store_str_on_list() {
|
fn store_str_on_list() {
|
||||||
let names_list = vec!["maria", "jacob", "kacper"];
|
let names_list = vec![Some("maria"), Some("jacob"), None, Some("kacper"), None];
|
||||||
let stringified = stringify(&names_list);
|
let only_values = into_dispose_nulls(names_list);
|
||||||
assert_eq!(stringified, "[maria, jacob, kacper]".to_string());
|
assert_eq!(only_values.len(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn store_numbers_on_list() {
|
fn store_numbers_on_list() {
|
||||||
let numbers_list = vec![1, 2, 3];
|
let numbers_list = vec![Some(1), Some(2), None, Some(3)];
|
||||||
let stringified = stringify(&numbers_list);
|
let only_values = into_dispose_nulls(numbers_list);
|
||||||
assert_eq!(stringified, "[1, 2, 3]".to_string());
|
assert_eq!(only_values.len(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_custom_type_on_list() {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
struct Rectangle {
|
||||||
|
width: i32,
|
||||||
|
height: i32
|
||||||
|
}
|
||||||
|
impl Rectangle {
|
||||||
|
fn new(width: i32, height: i32) -> Self {
|
||||||
|
Self { width, height }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let custom_list = vec![Some(Rectangle::new(1, 2)), None, None, Some(Rectangle::new(3, 4))];
|
||||||
|
let only_values = into_dispose_nulls(custom_list);
|
||||||
|
assert_eq!(only_values.len(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -746,13 +746,10 @@ name = "generics3"
|
||||||
dir = "14_generics"
|
dir = "14_generics"
|
||||||
hint = """
|
hint = """
|
||||||
Vectors in Rust use generics to create dynamically-sized arrays of any type.
|
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.
|
The `into_dispose_nulls` 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.
|
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:
|
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.
|
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
|
||||||
|
|
|
@ -1,17 +1,13 @@
|
||||||
// generics3.rs
|
// generics3.rs
|
||||||
// Execute `rustlings hint generics3` or use the `hint` watch subcommand for a hint.
|
// 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
|
// Here we added generic type `T` to function signature
|
||||||
fn stringify<T>(list: &[T]) -> String
|
// Now this function can be used with vector of any
|
||||||
where
|
fn into_dispose_nulls<T>(list: Vec<Option<T>>) -> Vec<T> {
|
||||||
T: ToString // here we also specify that T needs to implement ToString trait so we can use .to_string() on the vetor elements
|
list
|
||||||
{
|
.into_iter()
|
||||||
let items_str = list
|
.flatten()
|
||||||
.iter()
|
.collect()
|
||||||
.map(|e| e.to_string())
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
.join(", ");
|
|
||||||
format!("[{items_str}]")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -24,15 +20,32 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn store_str_on_list() {
|
fn store_str_on_list() {
|
||||||
let names_list = vec!["maria", "jacob", "kacper"];
|
let names_list = vec![Some("maria"), Some("jacob"), None, Some("kacper"), None];
|
||||||
let stringified = stringify(&names_list);
|
let only_values = into_dispose_nulls(names_list);
|
||||||
assert_eq!(stringified, "[maria, jacob, kacper]".to_string());
|
assert_eq!(only_values.len(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn store_numbers_on_list() {
|
fn store_numbers_on_list() {
|
||||||
let numbers_list = vec![1, 2, 3];
|
let numbers_list = vec![Some(1), Some(2), None, Some(3)];
|
||||||
let stringified = stringify(&numbers_list);
|
let only_values = into_dispose_nulls(numbers_list);
|
||||||
assert_eq!(stringified, "[1, 2, 3]".to_string());
|
assert_eq!(only_values.len(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_custom_type_on_list() {
|
||||||
|
struct Rectangle {
|
||||||
|
width: i32,
|
||||||
|
height: i32
|
||||||
|
}
|
||||||
|
impl Rectangle {
|
||||||
|
fn new(width: i32, height: i32) -> Self {
|
||||||
|
Self { width, height }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let custom_list = vec![Some(Rectangle::new(1, 2)), None, None, Some(Rectangle::new(3, 4))];
|
||||||
|
let only_values = into_dispose_nulls(custom_list);
|
||||||
|
assert_eq!(only_values.len(), 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue