Struct const_format::AsciiStr[][src]

pub struct AsciiStr<'a>(_);
This is supported on crate feature fmt only.
Expand description

An ascii string slice.

You can also construct an AsciiStr at compile-time with the ascii_str macro, erroring at compile if the constant isn’t ascii.

Example

use const_format::wrapper_types::{AsciiStr, NotAsciiError};
use const_format::ascii_str;

const HELLO: AsciiStr = unwrap_ascii(AsciiStr::new(b"hello"));
const EURO: AsciiStr = unwrap_ascii(AsciiStr::new("foo €".as_bytes()));

assert_eq!(HELLO.as_str(), "hello");
assert_eq!(EURO.as_str(), "<error>");
assert_eq!(AsciiStr::new("foo €".as_bytes()), Err(NotAsciiError{invalid_from: 4}));

const fn unwrap_ascii(res: Result<AsciiStr<'_>, NotAsciiError>) -> AsciiStr<'_> {
    match res {
        Ok(x) => x,
        Err(_) => ascii_str!("<error>"),
    }
}

Implementations

Constructs this AsciiStr from a possibly non-ascii str slice.

Returns a NonAsciiError error on the first non-ascii byte.

Example

use const_format::wrapper_types::{AsciiStr, NotAsciiError};

let ok = AsciiStr::from_str("foo bar").unwrap();

assert_eq!(ok.as_str(), "foo bar");
assert_eq!(AsciiStr::from_str("foo bar ½"), Err(NotAsciiError{invalid_from: 8}));

Constructs this AsciiStr from a possibly non-ascii byte slice.

Returns a NonAsciiError error on the first non-ascii byte.

Example

use const_format::wrapper_types::{AsciiStr, NotAsciiError};

let ok = AsciiStr::new(b"foo bar").unwrap();

assert_eq!(ok.as_str(), "foo bar");
assert_eq!(AsciiStr::new(b"foo bar \x80"), Err(NotAsciiError{invalid_from: 8}));

Constructs an empty AsciiStr

Example

use const_format::AsciiStr;

assert_eq!(AsciiStr::empty().as_str(), "");

Queries the length of the AsciiStr

Example

use const_format::{AsciiStr, ascii_str};

assert_eq!(AsciiStr::empty().len(), 0);
assert_eq!(ascii_str!("hello").len(), 5);

Queries whether this AsciiStr is empty.

Example

use const_format::{AsciiStr, ascii_str};

assert_eq!(AsciiStr::empty().is_empty(), true);
assert_eq!(ascii_str!("hello").is_empty(), false);

Accessor for the wrapped ascii string.

Example

use const_format::{AsciiStr, ascii_str};

assert_eq!(AsciiStr::empty().as_bytes(), b"");
assert_eq!(ascii_str!("hello").as_bytes(), b"hello");

Accessor for the wrapped ascii string.

Example

use const_format::{AsciiStr, ascii_str};

assert_eq!(AsciiStr::empty().as_str(), "");
assert_eq!(ascii_str!("hello").as_str(), "hello");

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

What kind of type this is, this can be one of: Read more

The type after dereferencing, implemented as type This = Self; for all non-reference types Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.