Skip to main content

malachite_q/rational_vector/conversion/
into_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;
11use alloc::vec;
12use core::slice;
13
14impl IntoIterator for RationalVector {
15    type Item = Rational;
16    type IntoIter = vec::IntoIter<Rational>;
17
18    /// Converts a [`RationalVector`] into an iterator over its elements, taking the vector by
19    /// value.
20    ///
21    /// The elements are produced in order, starting with the element at index 0.
22    ///
23    /// # Worst-case complexity
24    /// Constant time and additional memory.
25    ///
26    /// # Examples
27    /// ```
28    /// use core::str::FromStr;
29    /// use malachite_q::rational_vector::RationalVector;
30    ///
31    /// let v = RationalVector::from_str("(1/2, -2, 3)").unwrap();
32    /// assert_eq!(
33    ///     v.into_iter().map(|x| x.to_string()).collect::<Vec<_>>(),
34    ///     ["1/2", "-2", "3"]
35    /// );
36    /// ```
37    #[inline]
38    fn into_iter(self) -> vec::IntoIter<Rational> {
39        self.elements.into_iter()
40    }
41}
42
43impl<'a> IntoIterator for &'a RationalVector {
44    type Item = &'a Rational;
45    type IntoIter = slice::Iter<'a, Rational>;
46
47    /// Converts a reference to a [`RationalVector`] into an iterator over references to its
48    /// elements.
49    ///
50    /// The elements are produced in order, starting with the element at index 0. This is what `for
51    /// x in &v` uses.
52    ///
53    /// # Worst-case complexity
54    /// Constant time and additional memory.
55    ///
56    /// # Examples
57    /// ```
58    /// use core::str::FromStr;
59    /// use malachite_q::rational_vector::RationalVector;
60    ///
61    /// let v = RationalVector::from_str("(1/2, -2, 3)").unwrap();
62    /// let mut strings = Vec::new();
63    /// for x in &v {
64    ///     strings.push(x.to_string());
65    /// }
66    /// assert_eq!(strings, ["1/2", "-2", "3"]);
67    /// ```
68    #[inline]
69    fn into_iter(self) -> slice::Iter<'a, Rational> {
70        self.elements.iter()
71    }
72}
73
74impl<'a> IntoIterator for &'a mut RationalVector {
75    type Item = &'a mut Rational;
76    type IntoIter = slice::IterMut<'a, Rational>;
77
78    /// Converts a mutable reference to a [`RationalVector`] into an iterator over mutable
79    /// references to its elements.
80    ///
81    /// The elements are produced in order, starting with the element at index 0. This is what `for
82    /// x in &mut v` uses; since every list of elements is a valid vector, any element may be
83    /// changed.
84    ///
85    /// # Worst-case complexity
86    /// Constant time and additional memory.
87    ///
88    /// # Examples
89    /// ```
90    /// use core::str::FromStr;
91    /// use malachite_base::num::basic::traits::One;
92    /// use malachite_q::Rational;
93    /// use malachite_q::rational_vector::RationalVector;
94    ///
95    /// let mut v = RationalVector::from_str("(1/2, -2, 3)").unwrap();
96    /// for x in &mut v {
97    ///     *x += Rational::ONE;
98    /// }
99    /// assert_eq!(v.to_string(), "(3/2, -1, 4)");
100    /// ```
101    #[inline]
102    fn into_iter(self) -> slice::IterMut<'a, Rational> {
103        self.elements.iter_mut()
104    }
105}