Skip to main content
Version: dev

CtString

std::meta::ctstring contains methods on the built-in CtString type which is a compile-time, dynamically-sized string type. Compared to str<N> and fmtstr<N, T>, CtString is useful because its size does not need to be specified in its type. This can be used for formatting items at compile-time or general string handling in comptime code.

Since fmtstrs can be converted into CtStrings, you can make use of their formatting abilities in CtStrings by formatting in fmtstrs then converting the result to a CtString afterward.

Traits

AsCtString

as-ctstring
pub trait AsCtString {
comptime fn as_ctstring(self) -> CtString;
}

Source code: noir_stdlib/src/meta/ctstring.nr#L43-L47

Converts an object into a compile-time string.

Implementations:

impl<let N: u32> AsCtString for str<N> { ... }
impl<let N: u32, T> AsCtString for fmtstr<N, T> { ... }

Methods

new

new
pub comptime fn new() -> Self {

Source code: noir_stdlib/src/meta/ctstring.nr#L4-L6

Creates an empty CtString.

append_str

append_str
pub comptime fn append_str<let N: u32>(self, s: str<N>) -> Self {

Source code: noir_stdlib/src/meta/ctstring.nr#L11-L13

Returns a new CtString with the given str appended onto the end.

append_fmtstr

append_fmtstr
pub comptime fn append_fmtstr<let N: u32, T>(self, s: fmtstr<N, T>) -> Self {

Source code: noir_stdlib/src/meta/ctstring.nr#L17-L19

Returns a new CtString with the given fmtstr appended onto the end.

as_quoted_str

as_quoted_str
pub comptime fn as_quoted_str(self) -> Quoted {

Source code: noir_stdlib/src/meta/ctstring.nr#L26-L28

Returns a quoted string literal from this string's contents.

There is no direct conversion from a CtString to a str<N> since the size would not be known. To get around this, this function can be used in combination with macro insertion (!) to insert this string literal at this function's call site.

Example:

as_quoted_str_example
let my_ctstring = "foo bar".as_ctstring();
let my_str = my_ctstring.as_quoted_str!();

assert_eq(crate::meta::type_of(my_str), quote { str<7> }.as_type());

Source code: noir_stdlib/src/meta/ctstring.nr#L92-L97

Trait Implementations

impl Eq for CtString
impl Hash for CtString
impl Append for CtString