A heap-allocated string.
It is a wrapper around a Vector of bytes. In addition to the methods provided by Vector, it provides protocol implementations for Formatter (allowing it to be a sink for write), Hashable, Equatable and Comparable.
Methods
impl StringBuf { ... }
-
fn new() -> StringBuf
Create an empty string buffer.
This will not allocate until the first byte is inserted.
-
Create a string buffer from an existing vector.
-
Create a string buffer from a raw slice and a length.
The slice must be allocated with alloc and its length must be greater than or equal to
length
. -
Create a string buffer that can hold up to
capacity
bytes without reallocating. -
Create a string buffer from a slice, copying the bytes.
Example
use std::string::StringBuf; let str = StringBuf::from_slice("Hello, world!"); assert_eq!(str.len(), 13); assert_eq!(str[..] as &[u8], "Hello, world!");
Run this example -
Create a string buffer from an iterator of bytes.
See also to_vector for a chainable version.
Example
use std::string::StringBuf; let str = StringBuf::from_iter(&('A'..='F')); assert_eq!(str.len(), 6); assert_eq!(str[..] as &[u8], "ABCDEF");
Run this example -
Reserve capacity for at least
additional
bytes. -
Extend the string buffer with the bytes from a slice.
Example
use std::string::StringBuf; let str = StringBuf::new(); str.extend_from_slice("Hello, "); str.extend_from_slice("world!"); assert_eq!(str.len(), 13); assert_eq!(str[..] as &[u8], "Hello, world!");
Run this example -
Extend the string buffer with the bytes from an iterator.
-
Insert an byte at the given index, shifting all bytes after it.
Example
use std::string::StringBuf; let str = StringBuf::from_slice("luck"); str.insert(0, 'p'); assert_eq!(str.len(), 5); assert_eq!(str[..] as &[u8], "pluck");
Run this example -
Get the byte at the given index.
If the index is out of bounds, it returns
Option::none()
. -
Push an byte to the end of the string buffer.
-
Pop an byte from the end of the string buffer.
If the string buffer is empty, it returns
Option::none()
. -
Return the length of the string buffer.
-
Return the size of the underlying buffer.
-
Returns a region of memory "after the last byte".
This can be used to efficiently append bytes to the string buffer. The memory may not be initialized.
-
Returns
true
if the string buffer is empty,false
otherwise. -
fn clear(self: &mut StringBuf)
Clear the string buffer, removing all bytes.
Does not remove excess capacity, call shrink_to_fit afterwards, if this is desired.
-
Truncate the string buffer, removing all but the first
new_size
bytes.new_size
should not be greater than the current capacity of the string buffer, but it may be greater than the current length of the string buffer. In this case the string buffer will grow to the specified capacity.str.truncate(0)
is equivalent tostr.clear()
. -
fn iter(self: &StringBuf) -> SliceIterator<&u8>
-
fn iter_ref(self: &StringBuf) -> SliceRefIterator<&u8>
-
fn iter_mut(self: &mut StringBuf) -> SliceRefIterator<&mut u8>
Returns an iterator over mutable pointers to the elements of the collection.
See IterableMut for details.
-
fn shrink_to_fit(self: &mut StringBuf)
Shrink the underlying buffer to the length of the string buffer.
-
fn free(self: &mut StringBuf)
Frees the memory backing the object.
-
Returns a copy of the object.
-
Write a string
-
Write a single character
-
Returns a view to the object.
-
Returns a mutable view to the object.
Mixins
impl StringBuf { ... }
-
-
Returns
false
if arguments are equal,true
otherwise -
mixin Comparable<StringBuf>
-
Returns
true
iflhs
is strictly less thanrhs
,false
otherwise -
Returns
true
iflhs
is less or equal torhs
,false
otherwise -
Returns
true
iflhs
strictly greater thanrhs
,false
otherwise -
Returns
true
iflhs
greater than or equalrhs
,false
otherwise -