Maniplate `&'static str` (e.g., `concat!`, `format!`) in Rust without pain!

Overview

static_str_ops

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.

Note
With this crate, the staticized strings will leaked and the reference is hold by the underlying HashSet. The destaticize() method can be used to released the previously added strings.

crates.io Downloads Docs.rs Github Actions

APIs

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.,

    Examples:

    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.

    Examples:

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

    Remove a string from the HashSet. Return true if the string was present and is successfully removed, false otherwise.

    Examples:

    let s: &'static str = staticize(&String::from("hello world!"));
    assert!(destaticize(s));
  • 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.

    Examples:

    let hello_world: &'static str = static_concat!("Hello", ", ", "world!");
  • 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.

    let name = "John";
    let age = 30;
    let message = static_format!("My name is {} and I'm {} years old.", name, age);
  • 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.

    Examples:

    let s: &'static str = staticize_once!({
        let s = "";  // can be some expensive computation
        s
    });

    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.

License

This project is licensed under the BSD-3 Clause license (LICENSE or http://opensource.org/licenses/BSD-3-Clause).

You might also like...
Simple Secure Static (HTTPS) File Server with embedded certificate

Secure Static File Server Static Files HTTPs server with self signed embedded certificate Installation Install using cargo: cargo install ssfs Or buil

A file server that supports static serving, uploading, searching, accessing control, webdav...
A file server that supports static serving, uploading, searching, accessing control, webdav...

Dufs Dufs is a distinctive utility file server that supports static serving, uploading, searching, accessing control, webdav... Features Serve static

A simple, fast, and easy to use static file server

Warning This is still in early development, I would not recommend for production use.. yet. See the issues for things that are on the "roadmap" or mis

A mini paste bin and url shortener written in rust without databases.

pb Build $ cargo build --release Environment Variables PB_DATA: /some/path (Default: ./pb_data) PB_SITE: Url of your site. (Default: http://localhost:

a Rust library implementing safe, lightweight context switches, without relying on kernel services

libfringe libfringe is a library implementing safe, lightweight context switches, without relying on kernel services. It can be used in hosted environ

A Python package written in Rust for email verification without sending any emails.

PyRustify PyRustify is a Python package written in Rust that verifies the email addresses. Features Feature Description Syntax validation Checks if th

Unborrowed Rust Compiler (rustc without a borrowchecker)

ubrustc: Unborrowed rustc This is rustc with the borrow checker disabled. I wrote it in like, 30 minutes because this tweet made me laugh. Example //

💫 This program allows you to do requests in Rust without reqwest !

Zapros 💫 This program allows you to do requests in Rust without reqwest ! Usage : Get use crate::http_client::HttpClient; use crate::http_client::Htt

Easy recursion in Rust, without stack overflows.

Recursive With recursive you can easily make (indirectly) recursive functions without worrying about stack overflows by marking them as #[recursive]:

Releases(v0.1.0)
Owner
Unsafe is not truely unsafe 😆
null
Tight Model format is a lossy 3D model format focused on reducing file size as much as posible without decreasing visual quality of the viewed model or read speeds.

What is Tight Model Format The main goal of the tmf project is to provide a way to save 3D game assets compressed in such a way, that there are no not

null 59 Mar 6, 2023
Given a set of kmers (fasta format) and a set of sequences (fasta format), this tool will extract the sequences containing the kmers.

Kmer2sequences Description Given a set of kmers (fasta / fastq [.gz] format) and a set of sequences (fasta / fastq [.gz] format), this tool will extra

Pierre Peterlongo 22 Sep 16, 2023
Kurzlink is a simple static site generator built in rust

kurzlink What is kurzlink? Kurzlink is a simple static site generator built in rust.

Evy Garden 2 Dec 15, 2022
Pure-Rust implementation of Fast Static Symbol Tables string compression

fsst-rs A pure-Rust, zero-dependency implementation of the FSST string compression algorithm. FSST is a string compression algorithm meant for use in

Spiral 78 Sep 29, 2024
Turn static CLI commands into TUIs with ease

lazycli Turn static CLI commands into TUIs with ease Demo: Usage Pick a command that spits out either a list or table of content, like ls, docker ps,

Jesse Duffield 255 Dec 20, 2022
Shellcheck - a static analysis tool for shell scripts

ShellCheck - A shell script static analysis tool ShellCheck is a GPLv3 tool that gives warnings and suggestions for bash/sh shell scripts: The goals o

Vidar Holen 31.1k Jan 9, 2023
⚙️ A curated list of static analysis (SAST) tools for all programming languages, config files, build tools, and more.

This repository lists static analysis tools for all programming languages, build tools, config files and more. The official website, analysis-tools.de

Analysis Tools 10.7k Jan 2, 2023
A fast static code analyzer & language server for Python

pylyzer ⚡ pylyzer is a static code analyzer / language server for Python written in Rust. Installation cargo (rust package manager) cargo install pyly

Shunsuke Shibayama 78 Jan 3, 2023
A work-in-progress static analyser.

Statan Statan is an early-stage static analyser for PHP and PXP projects. It is being developed in public and the journey is documented on my blog. Th

PXP 12 Jan 30, 2023
A opinionated and fast static analyzer for PHP.

TLDR; A static analyzer for PHP. It helps you catch common mistakes in your PHP code. These are the current checks implemented. Extending undefined cl

Denzyl Dick 11 Mar 6, 2023