]> git.proxmox.com Git - rustc.git/blame - src/libcollections/macros.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libcollections / macros.rs
CommitLineData
85aaf69f 1// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
223e47cc
LB
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
1a4d82fc 11/// Creates a `Vec` containing the arguments.
85aaf69f
SL
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];
c34b1796 29/// assert_eq!(v, [1, 1, 1]);
85aaf69f
SL
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.
e9174d1e
SL
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.
c34b1796 41#[cfg(not(test))]
1a4d82fc 42#[macro_export]
85aaf69f 43#[stable(feature = "rust1", since = "1.0.0")]
54a0048b 44#[allow_internal_unstable]
1a4d82fc 45macro_rules! vec {
85aaf69f
SL
46 ($elem:expr; $n:expr) => (
47 $crate::vec::from_elem($elem, $n)
48 );
49 ($($x:expr),*) => (
54a0048b 50 <[_]>::into_vec(box [$($x),*])
c34b1796
AL
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)]
60macro_rules! vec {
61 ($elem:expr; $n:expr) => (
62 $crate::vec::from_elem($elem, $n)
63 );
64 ($($x:expr),*) => (
54a0048b 65 $crate::slice::into_vec(box [$($x),*])
85aaf69f 66 );
1a4d82fc 67 ($($x:expr,)*) => (vec![$($x),*])
223e47cc 68}
85aaf69f
SL
69
70/// Use the syntax described in `std::fmt` to create a value of type `String`.
71/// See `std::fmt` for more information.
72///
c34b1796 73/// # Examples
85aaf69f
SL
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")]
82macro_rules! format {
83 ($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))
84}