collection_tools/collection/
vector.rs

1#[ allow( unused_imports, clippy ::wildcard_imports ) ]
2use super :: *;
3
4#[ doc( inline ) ]
5#[ allow( unused_imports ) ]
6#[ allow( clippy ::pub_use ) ]
7pub use alloc ::vec :: *;
8
9#[ doc( inline ) ]
10#[ allow( unused_imports ) ]
11#[ allow( clippy ::pub_use ) ]
12pub use core ::slice :: { Iter, IterMut };
13
14/// Creates a `Vec` from a list of elements.
15///
16/// The `vec` macro simplifies the creation of a `Vec` with initial elements.
17///
18/// # Origin
19///
20/// This collection is reexported from `alloc`.
21///
22/// # Syntax
23///
24/// The macro can be called with a comma-separated list of elements. A trailing comma is optional.
25///
26/// ```rust
27/// # use collection_tools :: { Vec, vec };
28/// // Vec of i32
29/// let vec1 = vec!( 1, 2, 3, 4, 5 );
30///
31/// // Vec of &str
32/// let vec2 = vec!{ "hello", "world", "rust" };
33///
34/// // With trailing comma
35/// let vec3 = vec!( 1.1, 2.2, 3.3, );
36/// ```
37///
38/// # Parameters
39///
40/// - `$( $key: expr ),* $( , )?` : A comma-separated list of elements to insert into the `Vec`.
41///   Each element can be of any type that implements the `Into< T >` trait, where `T` is the
42///   type stored in the `Vec`.
43///
44/// # Returns
45///
46/// Returns a `Vec` containing all the specified elements. The capacity of the vector is
47/// automatically determined based on the number of elements provided.
48///
49/// # Example
50///
51/// Basic usage with integers :
52///
53/// ```rust
54/// # use collection_tools :: { Vec, vec };
55/// let vec = vec!( 1, 2, 3 );
56/// assert_eq!( vec[ 0 ], 1 );
57/// assert_eq!( vec[ 1 ], 2 );
58/// assert_eq!( vec[ 2 ], 3 );
59/// ```
60///
61/// # Example
62///
63/// Creating a `Vec` of `&str` from string literals :
64///
65/// ```rust
66/// # use collection_tools :: { Vec, vec };
67/// let mixed = vec!{ "value", "another value" };
68/// assert_eq!( mixed[ 0 ], "value" );
69/// assert_eq!( mixed[ 1 ], "another value" );
70/// ```
71///
72#[ cfg( feature = "collection_constructors" ) ]
73#[ macro_export( local_inner_macros ) ]
74macro_rules! vec
75{
76  (
77  $( $key: expr ),* $( , )?
78 )
79  =>
80  {{
81  let _cap = count!( @count $( $key ),* );
82  let mut _vec = $crate ::collection ::Vec ::with_capacity( _cap );
83  $(
84   _vec.push( $key );
85 )*
86  _vec
87 }};
88}
89
90/// Creates a `Vec` from a list of elements.
91///
92/// The `into_vec!` macro simplifies the creation of a `Vec` with initial elements.
93/// Elements passed to the macro are automatically converted into the vector's element type
94/// using `.into()`, making it convenient to use literals or values of different, but convertible types.
95///
96/// Note: The `into_vec!` macro utilizes the `.into()` method to convert each element into the target type
97/// of the `Vec`. Therefore, the elements must be compatible with the `Into< T >` trait for the
98/// type `T` used in the `Vec`. Also, this means that sometimes you must specify the type of collection's items.
99///
100/// # Origin
101///
102/// This collection is reexported from `alloc`.
103///
104/// # Syntax
105///
106/// The macro can be called with a comma-separated list of elements. A trailing comma is optional.
107///
108/// ```rust
109/// # use collection_tools :: { Vec, into_vec };
110/// // Vec of i32
111/// let vec1: Vec< i32 > = into_vec!( 1, 2, 3, 4, 5 );
112///
113/// // Vec of String
114/// let vec2: Vec< String > = into_vec!{ "hello", "world", "rust" };
115///
116/// // With trailing comma
117/// let vec3: Vec< f64 > = into_vec!( 1.1, 2.2, 3.3, );
118/// ```
119///
120/// # Parameters
121///
122/// - `$( $key: expr ),* $( , )?` : A comma-separated list of elements to insert into the `Vec`.
123///   Each element can be of any type that implements the `Into< T >` trait, where `T` is the
124///   type stored in the `Vec`.
125///
126/// # Returns
127///
128/// Returns a `Vec` containing all the specified elements. The capacity of the vector is
129/// automatically determined based on the number of elements provided.
130///
131/// # Example
132///
133/// Basic usage with integers :
134///
135/// ```rust
136/// # use collection_tools :: { Vec, into_vec };
137/// let vec: Vec< i32 > = into_vec!( 1, 2, 3 );
138/// assert_eq!( vec[ 0 ], 1 );
139/// assert_eq!( vec[ 1 ], 2 );
140/// assert_eq!( vec[ 2 ], 3 );
141/// ```
142///
143/// # Example
144///
145/// Using with different types that implement `Into< T >` :
146///
147/// ```rust
148/// # use collection_tools :: { Vec, into_vec };
149/// let words: Vec< String > = into_vec!( "alpha", "beta", "gamma" );
150/// assert_eq!( words[ 0 ], "alpha" );
151/// assert_eq!( words[ 1 ], "beta" );
152/// assert_eq!( words[ 2 ], "gamma" );
153/// ```
154///
155/// # Example
156///
157/// Creating a `Vec` of `String` from string literals and String objects :
158///
159/// ```rust
160/// # use collection_tools :: { Vec, into_vec };
161/// let mixed: Vec< String > = into_vec!{ "value", "another value".to_string() };
162/// assert_eq!( mixed[ 0 ], "value" );
163/// assert_eq!( mixed[ 1 ], "another value" );
164/// ```
165///
166#[ cfg( feature = "collection_into_constructors" ) ]
167#[ macro_export( local_inner_macros ) ]
168macro_rules! into_vec
169{
170  (
171  $( $key: expr ),* $( , )?
172 )
173  =>
174  {{
175  let _cap = count!( @count $( $key ),* );
176  let mut _vec = $crate ::collection ::Vec ::with_capacity( _cap );
177  $(
178   _vec.push( Into ::into( $key ) );
179 )*
180  _vec
181 }};
182}