Skip to main content

malachite_base/unsigned_vector/conversion/string/
to_string.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;
11use core::fmt::{Debug, Display, Formatter, Result};
12
13impl<T: PrimitiveUnsigned> Display for UnsignedVector<T> {
14    /// Converts an [`UnsignedVector`] to a [`String`].
15    ///
16    /// The elements are written in order, as [`Display`] writes them, separated by `, ` and
17    /// enclosed in parentheses, as a vector's coordinates are written by hand. The 0-dimensional
18    /// vector is `()`.
19    ///
20    /// # Worst-case complexity
21    /// $T(n) = O(n)$
22    ///
23    /// $M(n) = O(1)$
24    ///
25    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
26    ///
27    /// # Examples
28    /// ```
29    /// use malachite_base::unsigned_vector::UnsignedVector;
30    ///
31    /// assert_eq!(
32    ///     UnsignedVector {
33    ///         elements: vec![1u32, 2, 3]
34    ///     }
35    ///     .to_string(),
36    ///     "(1, 2, 3)"
37    /// );
38    /// assert_eq!(
39    ///     UnsignedVector {
40    ///         elements: vec![5u8]
41    ///     }
42    ///     .to_string(),
43    ///     "(5)"
44    /// );
45    /// assert_eq!(
46    ///     UnsignedVector::<u32> {
47    ///         elements: Vec::new()
48    ///     }
49    ///     .to_string(),
50    ///     "()"
51    /// );
52    /// ```
53    fn fmt(&self, f: &mut Formatter) -> Result {
54        f.write_str("(")?;
55        for (i, x) in self.elements.iter().enumerate() {
56            if i != 0 {
57                f.write_str(", ")?;
58            }
59            Display::fmt(x, f)?;
60        }
61        f.write_str(")")
62    }
63}
64
65impl<T: PrimitiveUnsigned> Debug for UnsignedVector<T> {
66    /// Converts an [`UnsignedVector`] to a [`String`].
67    ///
68    /// This is the same as the [`Display::fmt`] implementation, so that a collection of
69    /// [`UnsignedVector`]s is written the same way its elements are displayed.
70    ///
71    /// # Worst-case complexity
72    /// $T(n) = O(n)$
73    ///
74    /// $M(n) = O(1)$
75    ///
76    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
77    ///
78    /// # Examples
79    /// ```
80    /// use malachite_base::strings::ToDebugString;
81    /// use malachite_base::unsigned_vector::UnsignedVector;
82    ///
83    /// let xs = vec![
84    ///     UnsignedVector {
85    ///         elements: vec![1u32, 2],
86    ///     },
87    ///     UnsignedVector {
88    ///         elements: Vec::new(),
89    ///     },
90    /// ];
91    /// assert_eq!(xs[0].to_debug_string(), "(1, 2)");
92    /// assert_eq!(xs.to_debug_string(), "[(1, 2), ()]");
93    /// ```
94    #[inline]
95    fn fmt(&self, f: &mut Formatter) -> Result {
96        Display::fmt(self, f)
97    }
98}