construe

Macro construe 

Source
macro_rules! construe {
    ($v:vis const $name:ident: & $($l:lifetime)? str = $f:expr) => { ... };
    ($v:vis const $name:ident: & $($l:lifetime)? [$t:ty] = $f:expr) => { ... };
    ($v:vis const $name:ident: [$t:ty; _] = $f:expr) => { ... };
    (&str => $f:expr) => { ... };
    (&[$t:ty] => $f:expr) => { ... };
    ([$t:ty; _] => $f:expr) => { ... };
}
Expand description

Invoke a function returning Construe or StrConstrue

This will invoke the function (or any expression) twice: once to determine the size of the buffer needed, and once to collect its contents.

In case this is not already obvious to you: the expression must be a constant expression. Even if you’re not using the form of the macro that defines a constant for you, the macro still needs to store the results of the expression, which means it must have a size known at compile time, which, because we use the expression itself to determine that size, means it must be evaluable at compile time.

Can be used in 2 different ways: either to define a const item or as an (anonymous) expression. Both require you to specify the type of the array/slice/&str, but in the former it is simply part of the constant item definition, so you don’t need to repeat yourself.

In both cases, the type you specify may be:

  • &str for expressions that return StrConstrue
  • [T; _] if you want an array, where T is your type and _ will be replaced by the macro
  • &[T] if you want a slice of T

It’s a very simple macro, so you can easily implement your own version if you need it to be slightly different. But you most likely will want to use a macro since otherwise you need to write the entire expression (i.e. the function you call) twice, which would be pretty bad if there’s any arguments to it.