malachite_base/unsigned_vector/conversion/from_iterator.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::num::basic::unsigneds::PrimitiveUnsigned;
10use crate::unsigned_vector::UnsignedVector;
11
12impl<T: PrimitiveUnsigned> FromIterator<T> for UnsignedVector<T> {
13 /// Collects an iterator of elements into an [`UnsignedVector`].
14 ///
15 /// The vector's dimension is the number of elements the iterator produces, and every finite
16 /// iterator gives a valid vector.
17 ///
18 /// # Worst-case complexity
19 /// $T(n) = O(n)$
20 ///
21 /// $M(n) = O(n)$
22 ///
23 /// where $T$ is time, $M$ is additional memory, and $n$ is the number of elements produced, not
24 /// counting the cost of producing them.
25 ///
26 /// # Examples
27 /// ```
28 /// use malachite_base::unsigned_vector::UnsignedVector;
29 ///
30 /// let v: UnsignedVector<u8> = (1..=3).collect();
31 /// assert_eq!(v.to_string(), "(1, 2, 3)");
32 /// let v: UnsignedVector<u8> = core::iter::empty().collect();
33 /// assert_eq!(v.to_string(), "()");
34 /// ```
35 #[inline]
36 fn from_iter<I: IntoIterator<Item = T>>(xs: I) -> Self {
37 Self {
38 elements: xs.into_iter().collect(),
39 }
40 }
41}