]> git.proxmox.com Git - rustc.git/blob - src/libcollections/macros.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / 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.
4 //
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.
10
11 /// Creates a `Vec` containing the arguments.
12 ///
13 /// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
14 /// There are two forms of this macro:
15 ///
16 /// - Create a `Vec` containing a given list of elements:
17 ///
18 /// ```
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);
23 /// ```
24 ///
25 /// - Create a `Vec` from a given element and size:
26 ///
27 /// ```
28 /// let v = vec![1; 3];
29 /// assert_eq!(v, [1, 1, 1]);
30 /// ```
31 ///
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
34 /// a constant.
35 ///
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
40 /// boxed integers.
41 #[cfg(not(test))]
42 #[macro_export]
43 #[stable(feature = "rust1", since = "1.0.0")]
44 #[allow_internal_unstable]
45 macro_rules! vec {
46 ($elem:expr; $n:expr) => (
47 $crate::vec::from_elem($elem, $n)
48 );
49 ($($x:expr),*) => (
50 <[_]>::into_vec(box [$($x),*])
51 );
52 ($($x:expr,)*) => (vec![$($x),*])
53 }
54
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
59 #[cfg(test)]
60 macro_rules! vec {
61 ($elem:expr; $n:expr) => (
62 $crate::vec::from_elem($elem, $n)
63 );
64 ($($x:expr),*) => (
65 $crate::slice::into_vec(box [$($x),*])
66 );
67 ($($x:expr,)*) => (vec![$($x),*])
68 }
69
70 /// Use the syntax described in `std::fmt` to create a value of type `String`.
71 /// See `std::fmt` for more information.
72 ///
73 /// # Examples
74 ///
75 /// ```
76 /// format!("test");
77 /// format!("hello {}", "world!");
78 /// format!("x = {}, y = {y}", 10, y = 30);
79 /// ```
80 #[macro_export]
81 #[stable(feature = "rust1", since = "1.0.0")]
82 macro_rules! format {
83 ($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))
84 }