[][src]Struct smol_str::SmolStr

pub struct SmolStr(_);

A SmolStr is a string type that has the following properties:

  • size_of::<SmolStr>() == size_of::<String>()
  • Clone is O(1)
  • Strings are stack-allocated if they are:
    • Up to 22 bytes long
    • Longer than 22 bytes, but substrings of WS (see below). Such strings consist solely of consecutive newlines, followed by consecutive spaces
  • If a string does not satisfy the aforementioned conditions, it is heap-allocated

Unlike String, however, SmolStr is immutable. The primary use case for SmolStr is a good enough default storage for tokens of typical programming languages. Strings consisting of a series of newlines, followed by a series of whitespace are a typical pattern in computer programs because of indentation. Note that a specialized interner might be a better solution for some use cases.

Methods

impl SmolStr[src]

pub const fn new_inline_from_ascii(len: usize, bytes: &[u8]) -> SmolStr[src]

Constructs an inline variant of SmolStr at compile time.

Parameters

  • len: Must be short (≤ 22 bytes)
  • bytes: Must be ASCII bytes, and there must be at least len of them. If len is smaller than the actual len of bytes, the string is truncated.

Returns

A constant SmolStr with inline data.

Examples

const IDENT: SmolStr = SmolStr::new_inline_from_ascii(5, b"hello");

Given a len smaller than the number of bytes in bytes, the string is cut off:

const SHORT: SmolStr = SmolStr::new_inline_from_ascii(5, b"hello world");
assert_eq!(SHORT.as_str(), "hello");

Compile-time errors

This will fail at compile-time with a message like "index out of bounds" on a _len_is_short because the string is too large:

This example deliberately fails to compile
const IDENT: SmolStr = SmolStr::new_inline_from_ascii(
    49,
    b"hello world, how are you doing this fine morning?",
);

Similarly, this will fail to compile with "index out of bounds" on an _is_ascii binding because it contains non-ASCII characters:

This example deliberately fails to compile
const IDENT: SmolStr = SmolStr::new_inline_from_ascii(
    2,
    &[209, 139],
);

Last but not least, given a len that is larger than the number of bytes in bytes, it will fail to compile with "index out of bounds: the len is 5 but the index is 5" on a binding called byte:

This example deliberately fails to compile
const IDENT: SmolStr = SmolStr::new_inline_from_ascii(10, b"hello");

pub fn new<T>(text: T) -> SmolStr where
    T: Into<String> + AsRef<str>, 
[src]

pub fn as_str(&self) -> &str[src]

pub fn to_string(&self) -> String[src]

pub fn len(&self) -> usize[src]

pub fn is_empty(&self) -> bool[src]

pub fn is_heap_allocated(&self) -> bool[src]

Trait Implementations

impl<T> From<T> for SmolStr where
    T: Into<String> + AsRef<str>, 
[src]

impl From<SmolStr> for String[src]

impl Clone for SmolStr[src]

impl Default for SmolStr[src]

impl Eq for SmolStr[src]

impl Ord for SmolStr[src]

impl PartialEq<SmolStr> for SmolStr[src]

impl PartialEq<str> for SmolStr[src]

impl PartialEq<SmolStr> for str[src]

impl<'a> PartialEq<&'a str> for SmolStr[src]

impl<'a> PartialEq<SmolStr> for &'a str[src]

impl PartialEq<String> for SmolStr[src]

impl PartialEq<SmolStr> for String[src]

impl<'a> PartialEq<&'a String> for SmolStr[src]

impl<'a> PartialEq<SmolStr> for &'a String[src]

impl PartialOrd<SmolStr> for SmolStr[src]

impl Display for SmolStr[src]

impl Debug for SmolStr[src]

impl Deref for SmolStr[src]

type Target = str

The resulting type after dereferencing.

impl Hash for SmolStr[src]

impl FromIterator<char> for SmolStr[src]

impl FromIterator<String> for SmolStr[src]

impl<'a> FromIterator<&'a String> for SmolStr[src]

impl<'a> FromIterator<&'a str> for SmolStr[src]

impl Borrow<str> for SmolStr[src]

Auto Trait Implementations

impl Send for SmolStr

impl Sync for SmolStr

impl Unpin for SmolStr

impl UnwindSafe for SmolStr

impl RefUnwindSafe for SmolStr

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]