Skip to main content

NonEmptyString

Struct NonEmptyString 

Source
pub struct NonEmptyString(/* private fields */);
Expand description

A string that is guaranteed to contain at least one element.

NonEmptyString provides a safe wrapper around String that maintains the invariant that the string can never be empty.

§Examples

use non_non_full::NonEmptyString;

let string = "Hello, World!".to_owned();
let non_empty = NonEmptyString::new(string).unwrap();

// Empty String returns None
assert!(NonEmptyString::new(String::new()).is_none());

Implementations§

Source§

impl NonEmptyString

Source

pub fn new(string: String) -> Option<Self>

Creates a new NonEmptyString from a String. Returns None if the input String is empty.

Source

pub fn as_string(&self) -> &String

Gets a reference to the underlying String

Source

pub fn into_string(self) -> String

Converts the NonEmptyString back into a String, consuming self

Source

pub fn as_str(&self) -> &str

Returns a string slice containing the entire string

Source

pub fn push(&mut self, ch: char)

Pushes a char onto the end of the string

Source

pub fn push_str(&mut self, string: &str)

Pushes a string slice onto the end of the string

Source

pub fn insert(&mut self, idx: usize, ch: char)

Inserts a char at the given byte index

Source

pub fn insert_str(&mut self, idx: usize, string: &str)

Inserts a string slice at the given byte index

Source

pub fn remove(&mut self, idx: usize) -> Option<char>

Removes the char at the given byte index.

Returns None if removing would make the string empty.

Source

pub fn pop(&mut self) -> Option<char>

Removes the last char from the string.

Returns None if this would make the string empty.

Source

pub fn len(&self) -> usize

Returns the length of the string in bytes

Source

pub fn clear_except_first(&mut self)

Clears all chars except the first one

Source

pub fn chars(&self) -> Chars<'_>

Returns an iterator over the chars in the string

Source

pub fn char_indices(&self) -> CharIndices<'_>

Returns an iterator over the char indices in the string

Trait Implementations§

Source§

impl Clone for NonEmptyString

Source§

fn clone(&self) -> NonEmptyString

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NonEmptyString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for NonEmptyString

Source§

impl Hash for NonEmptyString

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl PartialEq for NonEmptyString

Source§

fn eq(&self, other: &NonEmptyString) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for NonEmptyString

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.