]>
git.proxmox.com Git - rustc.git/blob - src/libcollections/macros.rs
1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /// Creates a `Vec` containing the arguments.
13 /// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
14 /// There are two forms of this macro:
16 /// - Create a `Vec` containing a given list of elements:
19 /// let v = vec![1, 2, 3];
20 /// assert_eq!(v[0], 1);
21 /// assert_eq!(v[1], 2);
22 /// assert_eq!(v[2], 3);
25 /// - Create a `Vec` from a given element and size:
28 /// let v = vec![1; 3];
29 /// assert_eq!(v, [1, 1, 1]);
32 /// Note that unlike array expressions this syntax supports all elements
33 /// which implement `Clone` and the number of elements doesn't have to be
36 /// This will use `clone()` to duplicate an expression, so one should be careful
37 /// using this with types having a nonstandard `Clone` implementation. For
38 /// example, `vec![Rc::new(1); 5]` will create a vector of five references
39 /// to the same boxed integer value, not five references pointing to independently
43 #[stable(feature = "rust1", since = "1.0.0")]
44 #[allow_internal_unstable]
46 ($elem
:expr
; $n
:expr
) => (
47 $
crate::vec
::from_elem($elem
, $n
)
50 <[_
]>::into_vec(box [$
($x
),*])
52 ($
($x
:expr
,)*) => (vec
![$
($x
),*])
55 // HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
56 // required for this macro definition, is not available. Instead use the
57 // `slice::into_vec` function which is only available with cfg(test)
58 // NB see the slice::hack module in slice.rs for more information
61 ($elem
:expr
; $n
:expr
) => (
62 $
crate::vec
::from_elem($elem
, $n
)
65 $
crate::slice
::into_vec(box [$
($x
),*])
67 ($
($x
:expr
,)*) => (vec
![$
($x
),*])
70 /// Use the syntax described in `std::fmt` to create a value of type `String`.
71 /// See `std::fmt` for more information.
77 /// format!("hello {}", "world!");
78 /// format!("x = {}, y = {y}", 10, y = 30);
81 #[stable(feature = "rust1", since = "1.0.0")]
83 ($
($arg
:tt
)*) => ($
crate::fmt
::format(format_args
!($
($arg
)*)))