Skip to content

Allow macros to determine the length of multiplicative arguments #12782

Closed
@ghost

Description

This would allow http://static.rust-lang.org/doc/master/std/macros/macro.vec.html to use Vec::with_capacity to avoid unnecessary reallocations of the vector.

I'll be happy to take this on but I'm not entirely sure what a good syntax for this would be. I was thinking of:

macro_rules! vec(
    ($($e:expr),*) => ({
        // leading _ to allow empty construction without a warning.
        let mut _temp = ::std::vec_ng::Vec::with_capacity(#($e));
        $(_temp.push($e);)*
        _temp
    })
)

Since the individual elements in a multiplicative pattern can contain an arbitrary tree of expressions, # would recursively count all the elements that would expand to a specific $identifier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-syntaxextArea: Syntax extensions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions