rust_book14/
lib.rs

1//! `Rust` book lessons.
2//! Chapter [14](https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html).
3
4pub mod slices {
5    //! Slices utils.
6
7    /// Returns the contained first value or computes it from a closure.
8    /// See: [`Option::unwrap_or_else`]
9    ///
10    /// # Examples
11    ///
12    /// ```
13    /// let slice = [1, 2, 3];
14    /// let it = rust_book14::slices::get_first_or_else(&slice, || &-1);
15    /// assert_eq!(1, *it);
16    ///
17    /// let slice = [];
18    /// let it = rust_book14::slices::get_first_or_else(&slice, || &-1);
19    /// assert_eq!(-1, *it);
20    /// ```
21    pub fn get_first_or_else<'a, T, F: FnOnce() -> &'a T>(slice: &'a [T], f: F) -> &'a T {
22        if slice.is_empty() {
23            return f();
24        }
25        return &slice[0];
26        // return slice.get(0).unwrap_or_else(f);
27    }
28}
29
30/// Returns the contained first value or computes it from a closure.
31/// See: [`Option::unwrap_or_else`]
32///
33/// # Examples
34///
35/// ```
36/// let list = vec![1, 2, 3];
37/// let answer = rust_book14::get_first_or_else(&list, || &-1);
38/// assert_eq!(1, *answer);
39///
40/// let list = vec![];
41/// let answer = rust_book14::get_first_or_else(&list, || &-1);
42/// assert_eq!(-1, *answer);
43/// ```
44pub fn get_first_or_else<'a, T, F: FnOnce() -> &'a T>(list: &'a Vec<T>, f: F) -> &'a T {
45    if list.is_empty() {
46        return f();
47    }
48    return &list[0];
49    // return list.get(0).unwrap_or_else(f);
50}
51
52/// Returns the contained first value or a provided default.
53/// See: [`Option::unwrap_or`]
54///
55/// # Examples
56///
57/// ```
58/// let list = vec![1, 2, 3];
59/// let answer = rust_book14::get_first_or(&list, &-1);
60/// assert_eq!(1, *answer);
61///
62/// let list = vec![];
63/// let answer = rust_book14::get_first_or(&list, &-1);
64/// assert_eq!(-1, *answer);
65/// ```
66pub fn get_first_or<'a, T>(list: &'a Vec<T>, default: &'a T) -> &'a T {
67    if list.is_empty() {
68        return default;
69    }
70    return &list[0];
71    // return list.get(0).unwrap_or(default);
72}
73
74/// [`Some(value)`]: Some
75/// [`Err(err)`]: Err
76///
77/// Returns the contained first [`Some(value)`] or a provided [`Err(err)`].
78/// See: [`Option::ok_or`]
79///
80/// # Errors
81///
82/// If the `list` is empty, an [`Err(err)`] will be returned.
83///
84/// # Examples
85///
86/// ```
87/// let list = vec![1, 2, 3];
88/// let answer = rust_book14::get_first_or_error(&list, "foo");
89/// assert_eq!(Ok(&1), answer);
90///
91/// let list: Vec<u8> = vec![];
92/// let answer = rust_book14::get_first_or_error(&list, "foo");
93/// assert_eq!(Err("foo"), answer);
94/// ```
95pub fn get_first_or_error<T, U>(list: &Vec<T>, err: U) -> Result<&T, U> {
96    if list.is_empty() {
97        return Err(err);
98    }
99    return Ok(&list[0]);
100    // return list.get(0).ok_or(err);
101}
102
103/// Returns the contained first value or panics with a `msg`.
104/// See: [`Option::expect`]
105///
106/// # Panics
107///
108/// If the `list` is empty, panics with a `msg`.
109///
110/// # Examples
111///
112/// ```
113/// let list = vec![1, 2, 3];
114/// let answer = rust_book14::get_first_or_panic(&list, "foo");
115/// assert_eq!(&1, answer);
116///
117/// let list: Vec<u8> = vec![];
118/// let result = std::panic::catch_unwind(|| {
119///     rust_book14::get_first_or_panic(&list, "foo")
120/// });
121/// assert!(result.is_err());
122/// ```
123pub fn get_first_or_panic<'a, T>(list: &'a Vec<T>, msg: &str) -> &'a T {
124    if list.is_empty() {
125        panic!("{msg}");
126    }
127    return &list[0];
128    // return list.get(0).expect(msg);
129}