Skip to main content

malachite_nz/integer_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::integer::Integer;
10use crate::integer_vector::IntegerVector;
11
12impl FromIterator<Integer> for IntegerVector {
13    /// Collects an iterator of [`Integer`]s into an [`IntegerVector`].
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_nz::integer::Integer;
29    /// use malachite_nz::integer_vector::IntegerVector;
30    ///
31    /// let v: IntegerVector = (-1..=1).map(Integer::from).collect();
32    /// assert_eq!(v.to_string(), "(-1, 0, 1)");
33    /// let v: IntegerVector = core::iter::empty().collect();
34    /// assert_eq!(v.to_string(), "()");
35    /// ```
36    #[inline]
37    fn from_iter<I: IntoIterator<Item = Integer>>(xs: I) -> Self {
38        Self {
39            elements: xs.into_iter().collect(),
40        }
41    }
42}