Struct heapless::String [] [src]

pub struct String<A> where
    A: Unsize<[u8]>, 
{ /* fields omitted */ }

A String backed by a fixed size heapless::Vec

Methods

impl<A> String<A> where
    A: Unsize<[u8]>, 
[src]

[src]

Constructs a new, empty String

Examples

Basic usage:

use heapless::String;

let mut s: String<[u8; 4]> = String::new();

[src]

Converts a vector of bytes into a String.

A string slice ([&str]) is made of bytes ([u8]), and a vector of bytes ([Vec<u8>]) is made of bytes, so this function converts between the two. Not all byte slices are valid Strings, however: String requires that it is valid UTF-8. from_utf8() checks to ensure that the bytes are valid UTF-8, and then does the conversion.

See std::String for further information.

Examples

Basic usage:

use heapless::{String, Vec};

let mut v: Vec<u8, [u8; 8]> = Vec::new();
v.push('a' as u8).unwrap();
v.push('b' as u8).unwrap();

let s = String::from_utf8(v).unwrap();
assert!(s.len() == 2);

Incorrect bytes:

use heapless::{String, Vec};

// some invalid bytes, in a vector

let mut v: Vec<u8, [u8; 8]> = Vec::new();
v.push(0).unwrap();
v.push(159).unwrap();
v.push(146).unwrap();
v.push(150).unwrap();
assert!(String::from_utf8(v).is_err());

[src]

Converts a vector of bytes to a String without checking that the string contains valid UTF-8.

See the safe version, from_utf8, for more details.

[src]

Converts a String into a byte vector.

This consumes the String, so we do not need to copy its contents.

Examples

Basic usage:

use heapless::String;

let s: String<[_; 4]> = String::from("ab");
let b = s.into_bytes();
assert!(b.len() == 2);

assert_eq!(&['a' as u8, 'b' as u8], &b[..]);

[src]

Extracts a string slice containing the entire string.

Examples

Basic usage:

use heapless::String;

let mut s: String<[_; 4]> = String::from("ab");
assert!(s.as_str() == "ab");

let _s = s.as_str();
// s.push('c'); // <- cannot borrow `s` as mutable because it is also borrowed as immutable

[src]

Converts a String into a mutable string slice.

Examples

Basic usage:

use heapless::String;

let mut s: String<[_; 4]> = String::from("ab");
let s = s.as_mut_str();
s.make_ascii_uppercase();

[src]

Appends a given string slice onto the end of this String.

Examples

Basic usage:

use heapless::String;

let mut s: String<[u8; 8]> = String::from("foo");

assert!(s.push_str("bar").is_ok());

assert_eq!("foobar", s);

assert!(s.push_str("tender").is_err());

[src]

Returns the maximum number of elements the String can hold

Examples

Basic usage:

use heapless::String;

let mut s: String<[u8; 4]> = String::new();
assert!(s.capacity() == 4);

[src]

Appends the given char to the end of this String.

Examples

Basic usage:

use heapless::String;

let mut s: String<[u8; 8]> = String::from("abc");

s.push('1').unwrap();
s.push('2').unwrap();
s.push('3').unwrap();

assert!("abc123" == s.as_str());

assert_eq!("abc123", s);

[src]

Returns a byte slice of this String's contents.

The inverse of this method is from_utf8.

Examples

Basic usage:

use heapless::String;

let s: String<[u8; 8]> = String::from("hello");

assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes());

[src]

Shortens this String to the specified length.

If new_len is greater than the string's current length, this has no effect.

Note that this method has no effect on the allocated capacity of the string

Panics

Panics if new_len does not lie on a char boundary.

Examples

Basic usage:

use heapless::String;

let mut s: String<[u8; 8]> = String::from("hello");

s.truncate(2);

assert_eq!("he", s);

[src]

Removes the last character from the string buffer and returns it.

Returns None if this String is empty.

Examples

Basic usage:

use heapless::String;

let mut s: String<[u8; 8]> = String::from("foo");

assert_eq!(s.pop(), Some('o'));
assert_eq!(s.pop(), Some('o'));
assert_eq!(s.pop(), Some('f'));

assert_eq!(s.pop(), None);

[src]

Returns true if this String has a length of zero.

Returns false otherwise.

Examples

Basic usage:

use heapless::String;

let mut v: String<[u8; 8]> = String::new();
assert!(v.is_empty());

v.push('a');
assert!(!v.is_empty());

[src]

Truncates this String, removing all contents.

While this means the String will have a length of zero, it does not touch its capacity.

Examples

Basic usage:

use heapless::String;

let mut s: String<[u8; 8]> = String::from("foo");

s.clear();

assert!(s.is_empty());
assert_eq!(0, s.len());
assert_eq!(8, s.capacity());

[src]

Returns the length of this String, in bytes.

Examples

Basic usage:

use heapless::String;

let a: String<[u8; 8]> = String::from("foo");

assert_eq!(a.len(), 3);

Trait Implementations

impl<'a, A> From<&'a str> for String<A> where
    A: Unsize<[u8]>, 
[src]

[src]

Performs the conversion.

impl<A> Debug for String<A> where
    A: Unsize<[u8]>, 
[src]

[src]

Formats the value using the given formatter. Read more

impl<A> Write for String<A> where
    A: Unsize<[u8]>, 
[src]

[src]

Writes a slice of bytes into this writer, returning whether the write succeeded. Read more

[src]

Writes a [char] into this writer, returning whether the write succeeded. Read more

1.0.0
[src]

Glue for usage of the [write!] macro with implementors of this trait. Read more

impl<A> Deref for String<A> where
    A: Unsize<[u8]>, 
[src]

The resulting type after dereferencing.

[src]

Dereferences the value.

impl<A> DerefMut for String<A> where
    A: Unsize<[u8]>, 
[src]

[src]

Mutably dereferences the value.

impl<A> AsRef<str> for String<A> where
    A: Unsize<[u8]>, 
[src]

[src]

Performs the conversion.

impl<A> AsRef<[u8]> for String<A> where
    A: Unsize<[u8]>, 
[src]

[src]

Performs the conversion.

impl<A, B> PartialEq<String<B>> for String<A> where
    A: Unsize<[u8]>,
    B: Unsize<[u8]>, 
[src]

[src]

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

[src]

This method tests for !=.

impl<'a, 'b, A> PartialEq<str> for String<A> where
    A: Unsize<[u8]>, 
[src]

[src]

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

[src]

This method tests for !=.

impl<'a, 'b, A> PartialEq<String<A>> for str where
    A: Unsize<[u8]>, 
[src]

[src]

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

[src]

This method tests for !=.

impl<'a, 'b, A> PartialEq<&'a str> for String<A> where
    A: Unsize<[u8]>, 
[src]

[src]

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

[src]

This method tests for !=.

impl<'a, 'b, A> PartialEq<String<A>> for &'a str where
    A: Unsize<[u8]>, 
[src]

[src]

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

[src]

This method tests for !=.

impl<A> Eq for String<A> where
    A: Unsize<[u8]>, 
[src]

Auto Trait Implementations

impl<A> Send for String<A> where
    A: Send

impl<A> Sync for String<A> where
    A: Sync