#const #concat #expressions #variables #byte

no-std constcat

concat! with support for const variables and expressions

8 releases (4 breaking)

0.5.0 Feb 29, 2024
0.4.1 Jan 24, 2024
0.4.0 Nov 3, 2023
0.3.1 Oct 10, 2023
0.1.1 Mar 30, 2022

#120 in Rust patterns

Download history 11279/week @ 2023-12-23 16455/week @ 2023-12-30 27235/week @ 2024-01-06 33760/week @ 2024-01-13 35735/week @ 2024-01-20 33127/week @ 2024-01-27 34509/week @ 2024-02-03 38051/week @ 2024-02-10 45083/week @ 2024-02-17 42913/week @ 2024-02-24 37036/week @ 2024-03-02 38748/week @ 2024-03-09 39950/week @ 2024-03-16 40245/week @ 2024-03-23 55275/week @ 2024-03-30 46026/week @ 2024-04-06

187,937 downloads per month
Used in 333 crates (6 directly)

MIT/Apache

14KB
97 lines

constcat

Crates.io Version Docs.rs Latest Build Status

std::concat! with support for const variables and expressions.

Works on stable Rust ✨.

🚀 Getting started

Add constcat to your Cargo manifest.

cargo add constcat

Import the macro using the following.

use constcat::concat;

🤸 Usage

String slices

concat! works exactly like std::concat!, concatenating &str literals into a static string slice, except you can now pass variables and constant expressions.

const CRATE_NAME: &str = env!("CARGO_PKG_NAME");
const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
const fn tada() -> &'static str { "🎉" }
const VERSION: &str = concat!(CRATE_NAME, " ", CRATE_VERSION, tada());

Byte slices

concat_bytes! works similarly to concat!, concatenating const &[u8] expressions and literals into a static byte slice.

const VERSION: u32 = 1;
const fn entries() -> &'static [u8] { b"example" }
const HEADER: &[u8] = concat_bytes!(&VERSION.to_le_bytes(), entries());

T slices

concat_slices! is the underlying macro used for both of the above, this can be used to concatenate const &[T] expressions into a static slice.

This macro requires the type of slice to be specified in the form [T]: before the comma separated expressions.

const MAGIC: &[i32; 4] = &[1, 3, 3, 7];
const VERSION: i32 = 1;
const HEADER: &[i32] = concat_slices!([i32]: MAGIC, &[0, VERSION]);

If the type is not a std integer, f32, f64, or char type then you must also provide an initializer expression with the type, in the form [init; T]: . This also works for custom types as long as the type and initializer expression is able to be specified in an array initializer expression.

const PRIMARIES: &'static [(u8, u8, u8)] = &[(255, 0, 0), (0, 255, 0), (0, 0, 255)];
const SECONDARIES: &'static [(u8, u8, u8)] = &[(255, 255, 0), (255, 0, 255), (0, 255, 255)];
const COLORS: &[(u8, u8, u8)] = concat_slices!([(0, 0, 0); (u8, u8, u8)]: PRIMARIES, SECONDARIES);

License

This project is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.

No runtime deps