mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 00:00:05 +03:00
Compare commits
10 commits
9341fc5044
...
e6c855c18e
Author | SHA1 | Date | |
---|---|---|---|
e6c855c18e | |||
dd0634c483 | |||
fc0cd8f0f8 | |||
d5cae8ff59 | |||
38016cb2d6 | |||
59e8f70e55 | |||
4c8365fe88 | |||
52af0674c1 | |||
938b90e5f2 | |||
55cc8584bd |
|
@ -116,6 +116,8 @@ bin = [
|
||||||
{ name = "generics1_sol", path = "../solutions/14_generics/generics1.rs" },
|
{ name = "generics1_sol", path = "../solutions/14_generics/generics1.rs" },
|
||||||
{ name = "generics2", path = "../exercises/14_generics/generics2.rs" },
|
{ name = "generics2", path = "../exercises/14_generics/generics2.rs" },
|
||||||
{ name = "generics2_sol", path = "../solutions/14_generics/generics2.rs" },
|
{ name = "generics2_sol", path = "../solutions/14_generics/generics2.rs" },
|
||||||
|
{ name = "generics3", path = "../exercises/14_generics/generics3.rs" },
|
||||||
|
{ name = "generics3_sol", path = "../solutions/14_generics/generics3.rs" },
|
||||||
{ name = "traits1", path = "../exercises/15_traits/traits1.rs" },
|
{ name = "traits1", path = "../exercises/15_traits/traits1.rs" },
|
||||||
{ name = "traits1_sol", path = "../solutions/15_traits/traits1.rs" },
|
{ name = "traits1_sol", path = "../solutions/15_traits/traits1.rs" },
|
||||||
{ name = "traits2", path = "../exercises/15_traits/traits2.rs" },
|
{ name = "traits2", path = "../exercises/15_traits/traits2.rs" },
|
||||||
|
|
54
exercises/14_generics/generics3.rs
Normal file
54
exercises/14_generics/generics3.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
// generics3.rs
|
||||||
|
// Execute `rustlings hint generics3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
|
// This function should take an array of `Option` elements and returns array of not None elements
|
||||||
|
// TODO fix this function signature
|
||||||
|
fn into_dispose_nulls(list: Vec<Option<&str>>) -> Vec<&str> {
|
||||||
|
list.into_iter().flatten().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_str_on_list() {
|
||||||
|
let names_list = vec![Some("maria"), Some("jacob"), None, Some("kacper"), None];
|
||||||
|
let only_values = into_dispose_nulls(names_list);
|
||||||
|
assert_eq!(only_values.len(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_numbers_on_list() {
|
||||||
|
let numbers_list = vec![Some(1), Some(2), None, Some(3)];
|
||||||
|
let only_values = into_dispose_nulls(numbers_list);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,9 +4,11 @@
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
#[allow(unused_variables, unused_assignments)]
|
#[allow(unused_variables, unused_assignments)]
|
||||||
fn main() {
|
fn main() {
|
||||||
let my_option: Option<()> = None;
|
let my_option: Option<&str> = None;
|
||||||
|
// Assume that you don't know the value of `my_option`.
|
||||||
|
// In the case of `Some`, we want to print its value.
|
||||||
if my_option.is_none() {
|
if my_option.is_none() {
|
||||||
println!("{:?}", my_option.unwrap());
|
println!("{}", my_option.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let my_arr = &[
|
let my_arr = &[
|
||||||
|
|
|
@ -745,6 +745,17 @@ 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 `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.
|
||||||
|
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]]
|
||||||
|
|
|
@ -29,6 +29,21 @@ impl ParsePosNonzeroError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// As an alternative solution, implementing the `From` trait allows for the
|
||||||
|
// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
|
||||||
|
// using the `?` operator, without the need to call `map_err`.
|
||||||
|
//
|
||||||
|
// ```
|
||||||
|
// let x: i64 = s.parse()?;
|
||||||
|
// ```
|
||||||
|
//
|
||||||
|
// Traits like `From` will be dealt with in later exercises.
|
||||||
|
impl From<ParseIntError> for ParsePosNonzeroError {
|
||||||
|
fn from(err: ParseIntError) -> Self {
|
||||||
|
ParsePosNonzeroError::ParseInt(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
|
|
||||||
|
|
53
solutions/14_generics/generics3.rs
Normal file
53
solutions/14_generics/generics3.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
// generics3.rs
|
||||||
|
// Execute `rustlings hint generics3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
|
// Here we added generic type `T` to function signature
|
||||||
|
// Now this function can be used with vector of any
|
||||||
|
fn into_dispose_nulls<T>(list: Vec<Option<T>>) -> Vec<T> {
|
||||||
|
list.into_iter().flatten().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_str_on_list() {
|
||||||
|
let names_list = vec![Some("maria"), Some("jacob"), None, Some("kacper"), None];
|
||||||
|
let only_values = into_dispose_nulls(names_list);
|
||||||
|
assert_eq!(only_values.len(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_numbers_on_list() {
|
||||||
|
let numbers_list = vec![Some(1), Some(2), None, Some(3)];
|
||||||
|
let only_values = into_dispose_nulls(numbers_list);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,11 +3,11 @@ use std::mem;
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
#[allow(unused_variables, unused_assignments)]
|
#[allow(unused_variables, unused_assignments)]
|
||||||
fn main() {
|
fn main() {
|
||||||
let my_option: Option<()> = None;
|
let my_option: Option<&str> = None;
|
||||||
// `unwrap` of an `Option` after checking if it is `None` will panic.
|
// `unwrap` of an `Option` after checking if it is `None` will panic.
|
||||||
// Use `if-let` instead.
|
// Use `if-let` instead.
|
||||||
if let Some(value) = my_option {
|
if let Some(value) = my_option {
|
||||||
println!("{value:?}");
|
println!("{value}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// A comma was missing.
|
// A comma was missing.
|
||||||
|
|
Loading…
Reference in a new issue