Struct heapless::String [−][src]
pub struct String<const N: usize> { /* fields omitted */ }
Expand description
A fixed capacity String
Implementations
impl<const N: usize> String<N>
[src]
impl<const N: usize> String<N>
[src]pub const fn new() -> Self
[src]
pub const fn new() -> Self
[src]Constructs a new, empty String
with a fixed capacity of N
Examples
Basic usage:
use heapless::String; // allocate the string on the stack let mut s: String<4> = String::new(); // allocate the string in a static variable static mut S: String<4> = String::new();
pub fn into_bytes(self) -> Vec<u8, N>
[src]
pub fn into_bytes(self) -> Vec<u8, N>
[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[..]);
pub fn as_str(&self) -> &str
[src]
pub fn as_str(&self) -> &str
[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
pub fn as_mut_str(&mut self) -> &mut str
[src]
pub fn as_mut_str(&mut self) -> &mut str
[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();
pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8, N>
[src]
pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8, N>
[src]Returns a mutable reference to the contents of this String
.
Safety
This function is unsafe because it does not check that the bytes passed
to it are valid UTF-8. If this constraint is violated, it may cause
memory unsafety issues with future users of the String
, as the rest of
the library assumes that String
s are valid UTF-8.
Examples
Basic usage:
let mut s = String::from("hello"); unsafe { let vec = s.as_mut_vec(); assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]); vec.reverse(); } assert_eq!(s, "olleh");
pub fn push_str(&mut self, string: &str) -> Result<(), ()>
[src]
pub fn push_str(&mut self, string: &str) -> Result<(), ()>
[src]Appends a given string slice onto the end of this String
.
Examples
Basic usage:
use heapless::String; let mut s: String<8> = String::from("foo"); assert!(s.push_str("bar").is_ok()); assert_eq!("foobar", s); assert!(s.push_str("tender").is_err());
pub fn capacity(&self) -> usize
[src]
pub fn capacity(&self) -> usize
[src]Returns the maximum number of elements the String can hold
Examples
Basic usage:
use heapless::String; let mut s: String<4> = String::new(); assert!(s.capacity() == 4);
pub fn truncate(&mut self, new_len: usize)
[src]
pub fn truncate(&mut self, new_len: usize)
[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<8> = String::from("hello"); s.truncate(2); assert_eq!("he", s);
pub fn pop(&mut self) -> Option<char>
[src]
pub fn pop(&mut self) -> Option<char>
[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<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);
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
[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<8> = String::from("foo"); s.clear(); assert!(s.is_empty()); assert_eq!(0, s.len()); assert_eq!(8, s.capacity());
Trait Implementations
impl<const N: usize> Write for String<N>
[src]
impl<const N: usize> Write for String<N>
[src]