Crate const_str_join

Crate const_str_join 

Source
Expand description

Helper crate to concat static strings during compilation.

Strings (&'static str) that are known during compile-time can’t currently easily concatenated nor joined with a seperator. This tiny crate aims to help with the situation by providing conviencne macros that execute the required steps.

Some of the limitations that have to be worked around are:

  • No const iterators, yet.
  • Allocating static memory must be done with a fixed and know size.
  • Writing into strings must be done “manually” bytewise as there is no copy implementation.
  • Byteslices aren’t available in const-context requiring manually indexing into the buffers.

All of those aren’t show-stoppers but don’t exactly enable idiomatic Rust code and require walking the thin line of whats possible and what isn’t.

Example usage:

use const_str_join::const_join;

const A: &'static str = "A";
const B: &'static str = "B";
const C: &'static str = "C";
const ALL: [&'static str; 3] = [A, B, C] ;
// declare a new &'static str with the final string
const ALL_JOINED: &'static str = const_join!(ALL, ",");
assert_eq!(ALL_JOINED, "A,B,C");

Macros§

const_join
Declares a new constant value name with the joined string of array and sep. Example usage:
joined_array
Returns a buffer ([u8; N]) that contains the joined string as bytes.