pub type ContentLenParseBuf = String<20>;
Aliased Type§
struct ContentLenParseBuf { /* private fields */ }
Implementations
Source§impl<const N: usize> String<N>
impl<const N: usize> String<N>
Sourcepub const fn new() -> String<N>
pub const fn new() -> String<N>
Constructs a new, empty String
with a fixed capacity of N
bytes
§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();
Sourcepub fn from_utf8(vec: Vec<u8, N>) -> Result<String<N>, Utf8Error>
pub fn from_utf8(vec: Vec<u8, N>) -> Result<String<N>, Utf8Error>
Convert UTF-8 bytes into a String
.
§Examples
Basic usage:
use heapless::{String, Vec};
let mut sparkle_heart = Vec::<u8, 4>::new();
sparkle_heart.extend_from_slice(&[240, 159, 146, 150]);
let sparkle_heart: String<4> = String::from_utf8(sparkle_heart)?;
assert_eq!("💖", sparkle_heart);
Invalid UTF-8:
use core::str::Utf8Error;
use heapless::{String, Vec};
let mut vec = Vec::<u8, 4>::new();
vec.extend_from_slice(&[0, 159, 146, 150]);
let e: Utf8Error = String::from_utf8(vec).unwrap_err();
assert_eq!(e.valid_up_to(), 1);
Sourcepub unsafe fn from_utf8_unchecked(vec: Vec<u8, N>) -> String<N>
pub unsafe fn from_utf8_unchecked(vec: Vec<u8, N>) -> String<N>
Convert UTF-8 bytes into a String
, without checking that the string
contains valid UTF-8.
§Safety
The bytes passed in must be valid UTF-8.
§Examples
Basic usage:
use heapless::{String, Vec};
let mut sparkle_heart = Vec::<u8, 4>::new();
sparkle_heart.extend_from_slice(&[240, 159, 146, 150]);
// Safety: `sparkle_heart` Vec is known to contain valid UTF-8
let sparkle_heart: String<4> = unsafe { String::from_utf8_unchecked(sparkle_heart) };
assert_eq!("💖", sparkle_heart);
Sourcepub fn into_bytes(self) -> Vec<u8, N>
pub fn into_bytes(self) -> Vec<u8, N>
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::try_from("ab")?;
let b = s.into_bytes();
assert!(b.len() == 2);
assert_eq!(&['a' as u8, 'b' as u8], &b[..]);
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Extracts a string slice containing the entire string.
§Examples
Basic usage:
use heapless::String;
let mut s: String<4> = String::try_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
Sourcepub fn as_mut_str(&mut self) -> &mut str
pub fn as_mut_str(&mut self) -> &mut str
Converts a String
into a mutable string slice.
§Examples
Basic usage:
use heapless::String;
let mut s: String<4> = String::try_from("ab")?;
let s = s.as_mut_str();
s.make_ascii_uppercase();
Sourcepub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8, N>
pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8, N>
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:
use heapless::String;
let mut s: String<8> = String::try_from("hello")?;
unsafe {
let vec = s.as_mut_vec();
assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]);
vec.reverse();
}
assert_eq!(s, "olleh");
Sourcepub fn push_str(&mut self, string: &str) -> Result<(), ()>
pub fn push_str(&mut self, string: &str) -> Result<(), ()>
Appends a given string slice onto the end of this String
.
§Examples
Basic usage:
use heapless::String;
let mut s: String<8> = String::try_from("foo")?;
assert!(s.push_str("bar").is_ok());
assert_eq!("foobar", s);
assert!(s.push_str("tender").is_err());
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
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);
Sourcepub fn push(&mut self, c: char) -> Result<(), ()>
pub fn push(&mut self, c: char) -> Result<(), ()>
Appends the given [char
] to the end of this String
.
§Examples
Basic usage:
use heapless::String;
let mut s: String<8> = String::try_from("abc")?;
s.push('1').unwrap();
s.push('2').unwrap();
s.push('3').unwrap();
assert!("abc123" == s.as_str());
assert_eq!("abc123", s);
Sourcepub fn truncate(&mut self, new_len: usize)
pub fn truncate(&mut self, new_len: usize)
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::try_from("hello")?;
s.truncate(2);
assert_eq!("he", s);
Sourcepub fn pop(&mut self) -> Option<char>
pub fn pop(&mut self) -> Option<char>
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::try_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);
Ok::<(), ()>(())
Sourcepub fn remove(&mut self, index: usize) -> char
pub fn remove(&mut self, index: usize) -> char
Removes a [char
] from this String
at a byte position and returns it.
Note: Because this shifts over the remaining elements, it has a worst-case performance of O(n).
§Panics
Panics if idx
is larger than or equal to the String
’s length,
or if it does not lie on a [char
] boundary.
§Examples
Basic usage:
use heapless::String;
let mut s: String<8> = String::try_from("foo").unwrap();
assert_eq!(s.remove(0), 'f');
assert_eq!(s.remove(1), 'o');
assert_eq!(s.remove(0), 'o');
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
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::try_from("foo")?;
s.clear();
assert!(s.is_empty());
assert_eq!(0, s.len());
assert_eq!(8, s.capacity());
Ok::<(), ()>(())