owned_str 0.1.2

Provide a stack allocated String for no-std or const environement
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented2 out of 2 items with examples
  • Size
  • Source code size: 59.68 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 6.82 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Baptistemontan/owned_str
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Baptistemontan

Owned Str

This crate offers a stack equivalent to a String where you can choose the buffer size.

This as 2 benefits:

  • No allocation, so it is 100% #![no_std].
  • A lot of functions can be marked const, so you can create/manipulate strings at compile time.
use owned_str::{OwnedStr, UnsizedStr};

const fn make_str() -> OwnedStr<16> {
    let mut buff = OwnedStr::new();
    // you can use `unsize_mut` to get a size erased handle
    let s: &mut UnsizedStr = buff.unsize_mut();
    s.push_str("hello");
    push_world(s);
    buff
}

const fn push_world(s: &mut UnsizedStr) {
    s.push(' ');
    s.push_str("world");
    s.pop();
}

const S: &str = make_str().as_str();

fn main() {
    assert_eq!(S, "hello worl");

    // allow stack based string formatting
    use std::fmt::Write;
    let mut buff = OwnedStr::<32>::new();
    write!(&mut buff, "format {} arguments", "some").unwrap();
    assert_eq!(buff, "format some arguments");
}

If you would like to see a String method not already implemented, or a str method that could be made const, feel free to open an issue on github and PR are welcome!