Crate static_str_ops

source ·
Expand description

The static_str_ops crate solves a longstanding issue about how to perform non-const string operations, e.g., format!(), concat!(), etc. and return static string, i.e., &'static str.

Internally, the crate uses a global static HashSet to store all the static strings, and return the reference to the string in the HashSet if the string has been staticized before.

This create provides the following macros and functions:

  • staticize(s: &str) -> &'static str

    Convert a string to a static string. If the string has been staticized before, return the reference to the string in the HashSet.

    This function is the most basic usage of this crate, e.g.,

    use static_str_ops::staticize;
    
    let s: &'static str = staticize(&String::from("hello world!"));
  • is_staticized(s: &str) -> bool

    Check if a string has been staticized before.

  • destaticize(s: &str) -> bool

    Remove a static string from the internal HashSet. Return true if was present.

  • static_concat!(s1: expr, s2: expr, ...) -> &'static str

    Concatenate multiple strings into a static string. The arguments can be either a string literal.

    Like concat!(), but returns a static string.

  • static_format!(s: expr, ...) -> &'static str

    Format a string into a static string. The arguments can be whatever the builtin macro format!() can accept.

    Like format!(), but returns a static string.

  • staticize_once!(expr: expr) -> &'static str

    Similar to staticize(), but the expr will be evaluated only once. Under the hood, std::sync::Once is used.

    The function will be useful if you have a function that want to return a static string, while the generate logic is non-trivial, and you want this process only happen once, e.g.,

    use static_str_ops::*;
    
    let make_string = || {
        staticize_once!({
            let s = "";  // can be some expensive computation
            s
        })
    };
    
    let s1: &'static str = make_string();
    let s2: &'static str = make_string();

    When you call make_string() for multiple times, the body will be guaranteed to be evaluated only once.

Re-exports

Macros

  • Concatenates the given string literals into a single static string slice.
  • A macro that takes a format string and arguments, and returns a static string slice.
  • Macro to generate a unique identifier for a given expression, which can be used to create a static variable that is initialized once with the result of the expression. This macro ensures that the expression is only evaluated once at runtime, and the result is stored in a static variable for future use.

Functions

  • Removes a static string from the internal set of static strings.
  • Checks if a given string is a static string.
  • Converts a string slice to a static string slice.