staticstr 0.0.1

A string type that can hold both static and owned strings
Documentation
  • Coverage
  • 40%
    2 out of 5 items documented1 out of 5 items with examples
  • Size
  • Source code size: 5.39 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.62 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • SergeyKasmy

staticstr

[StaticStr] - a string type that can handle both static and owned strings.

Overview

The [StaticStr] type is designed to optimize string handling in scenarios where most strings are static (known at compile time) but some need to be dynamically generated. It internally uses a Cow to avoid unnecessary allocations when working with static strings while still maintaining the flexibility to handle owned strings when needed.

Use Cases

  • Configuration values that are usually hardcoded but sometimes need to be generated
  • Message templates with occasional dynamic content
  • Any situation where you frequently use &'static str but occasionally need String

Example

use staticstr::StaticStr;

// Use with static strings - no allocation
let static_message: StaticStr = "Hello, World!".into();

// Use with owned strings - allocates only when needed
let dynamic_message: StaticStr = format!("Hello, {}!", "User").into();

// Both types can be used the same way
println!("{}", static_message);  // Hello, World!
println!("{}", dynamic_message); // Hello, User!

License: MPL-2.0