diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index e67329df7aec4..e726793a814db 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -348,12 +348,21 @@ macro_rules! try( ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) }) ) +/// Expands to an expression which evaluates to the number of +/// comma-separated expression parameters passed to it. +#[macro_export] +macro_rules! count_args( + ($e:expr $(,$e_rest:expr)*) => (1u + count_args!($($e_rest),*)); + () => (0u); + ($($e:expr),*,) => (count_args!($($e),*)) +) + /// Create a `std::vec::Vec` containing the arguments. #[macro_export] macro_rules! vec( ($($e:expr),*) => ({ // leading _ to allow empty construction without a warning. - let mut _temp = ::std::vec::Vec::new(); + let mut _temp = ::std::vec::Vec::with_capacity(count_args!($($e),*)); $(_temp.push($e);)* _temp });