RFC 2509: concat_bytes macro

libs (macros)

Summary

Add a macro concat_bytes!() to join byte sequences onto an u8 array, the same way concat!() currently supports for str literals.

Motivation

concat!() is convenient and useful to create compile time str literals from str, bool, numeric and char literals in the code. This RFC adds an equivalent capability for [u8] instead of str.

Guide-level explanation

The concat_bytes!() macro concatenates literals into a byte string literal (an expression of the type &[u8; N]). The following literal types are supported as inputs:

For example, concat_bytes!(42, b"va", b'l', [1, 2]) evaluates to [42, 118, 97, 108, 1, 2].

Drawbacks

None known.

Rationale and alternatives

concat! could instead be changed to sometimes produce byte literals instead of string literals, like a previous revision of this RFC proposed. This would make it hard to ensure the right output type is produced – users would have to use hacks like adding a dummy b"" argument to force a byte literal output.

An earlier version of this RFC proposed to support integer literals outside of arrays, but that was rejected since it would make the output of byte_concat!(123, b"\n") inconsistent with the equivalent concat! invocation.

Unresolved questions