Struct heapless::String[][src]

pub struct String<N> where
    N: ArrayLength<u8>, 
{ /* fields omitted */ }

A fixed capacity String

Methods

impl<N> String<N> where
    N: ArrayLength<u8>, 
[src]

Constructs a new, empty String with a fixed capacity of N

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

let mut s: String<U4> = String::new();

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};
use heapless::consts::*;

let mut v: Vec<u8, U8> = 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};
use heapless::consts::*;

// some invalid bytes, in a vector

let mut v: Vec<u8, U8> = 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());

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.

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;
use heapless::consts::*;

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

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

Extracts a string slice containing the entire string.

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

let mut s: String<U4> = 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

Converts a String into a mutable string slice.

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

let mut s: String<U4> = String::from("ab");
let s = s.as_mut_str();
s.make_ascii_uppercase();

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

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

let mut s: String<U8> = String::from("foo");

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

assert_eq!("foobar", s);

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

Returns the maximum number of elements the String can hold

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

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

Appends the given char to the end of this String.

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

let mut s: String<U8> = String::from("abc");

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

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

assert_eq!("abc123", s);

Returns a byte slice of this String's contents.

The inverse of this method is from_utf8.

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

let s: String<U8> = String::from("hello");

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

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;
use heapless::consts::*;

let mut s: String<U8> = String::from("hello");

s.truncate(2);

assert_eq!("he", s);

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

Returns None if this String is empty.

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

let mut s: String<U8> = 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);

Returns true if this String has a length of zero.

Returns false otherwise.

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

let mut v: String<U8> = String::new();
assert!(v.is_empty());

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

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;
use heapless::consts::*;

let mut s: String<U8> = String::from("foo");

s.clear();

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

Returns the length of this String, in bytes.

Examples

Basic usage:

use heapless::String;
use heapless::consts::*;

let a: String<U8> = String::from("foo");

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

Trait Implementations

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

Performs the conversion.

impl<N> Debug for String<N> where
    N: ArrayLength<u8>, 
[src]

Formats the value using the given formatter. Read more

impl<N> Write for String<N> where
    N: ArrayLength<u8>, 
[src]

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

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

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

impl<N> Deref for String<N> where
    N: ArrayLength<u8>, 
[src]

The resulting type after dereferencing.

Dereferences the value.

impl<N> DerefMut for String<N> where
    N: ArrayLength<u8>, 
[src]

Mutably dereferences the value.

impl<N> AsRef<str> for String<N> where
    N: ArrayLength<u8>, 
[src]

Performs the conversion.

impl<N> AsRef<[u8]> for String<N> where
    N: ArrayLength<u8>, 
[src]

Performs the conversion.

impl<N1, N2> PartialEq<String<N2>> for String<N1> where
    N1: ArrayLength<u8>,
    N2: ArrayLength<u8>, 
[src]

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

This method tests for !=.

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

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

This method tests for !=.

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

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

This method tests for !=.

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

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

This method tests for !=.

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

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

This method tests for !=.

impl<N> Eq for String<N> where
    N: ArrayLength<u8>, 
[src]

Auto Trait Implementations

impl<N> Send for String<N>

impl<N> Sync for String<N>