malachite_q/rational_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::Rational;
10use crate::rational_vector::RationalVector;
11
12impl FromIterator<Rational> for RationalVector {
13 /// Collects an iterator of [`Rational`]s into a [`RationalVector`].
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_q::Rational;
29 /// use malachite_q::rational_vector::RationalVector;
30 ///
31 /// let v: RationalVector = (1u32..=3).map(|d| Rational::from_unsigneds(1, d)).collect();
32 /// assert_eq!(v.to_string(), "(1, 1/2, 1/3)");
33 /// let v: RationalVector = core::iter::empty().collect();
34 /// assert_eq!(v.to_string(), "()");
35 /// ```
36 #[inline]
37 fn from_iter<I: IntoIterator<Item = Rational>>(xs: I) -> Self {
38 Self {
39 elements: xs.into_iter().collect(),
40 }
41 }
42}