pub struct String {
/// The bytes representing the characters of the string.
bytes: Bytes,
}
Expand description
A UTF-8 encoded growable string. It has ownership over its buffer.
Additional Information
WARNING: As this type is meant to be forward compatible with UTF-8, do not
add any mutation functionality or unicode input of any kind until char
is
implemented, codepoints are not guaranteed to fall on byte boundaries
Fields
bytes: Bytes
The bytes representing the characters of the string.
Implementations
fn as_bytes(self) -> Bytes
fn as_bytes(self) -> Bytes
Returns Bytes
giving a UTF-8 representation of the string.
Returns
- [Bytes] - A UTF-8 representation of the string.
Examples
use std::string::String;
fn foo() {
let mut string = String::new();
string.push(0u8);
let bytes = string.as_bytes();
assert(bytes.len() == 1);
assert(bytes.get(0).unwrap() == 0u8);
}
fn capacity(self) -> u64
fn capacity(self) -> u64
Gets the amount of memory on the heap allocated to the String
.
Returns
u64
- The number of characters theString
can hold without reallocating.
Examples
use std::string::String;
fn foo() {
let mut string = String::new();
assert(string.capacity() == 0);
string.push(0u8);
assert(string.capacity() == 1);
string.push(1u8);
assert(string.capacity() == 2);
}
fn clear(refmut self)
fn clear(refmut self)
Truncates this String
to a length of zero, clearing all content.
Examples
use std::string::String;
fn foo() {
let mut string = String::new();
string.push(0u8);
assert(!string.is_empty());
string.clear();
assert(string.is_empty());
}
fn from_ascii(bytes: Bytes) -> Self
fn from_ascii(bytes: Bytes) -> Self
Converts a vector of ASCII encoded bytes to a String
.
Additional Information
Each byte represents a single character, this supports ASCII but it does not support Unicode.
Arguments
bytes
- ASCII bytes which will be converted into aString
.
Returns
- [String] - A
String
containing the ASCII encoded bytes.
Examples
use std::string::String;
fn foo() {
let mut bytes = Bytes::new();
bytes.push(0u8);
bytes.push(1u8);
let string = String::from_ascii(bytes);
}
fn from_ascii_str(s: str) -> Self
fn from_ascii_str(s: str) -> Self
Converts a string slice containing ASCII encoded bytes to a String
Arguments
s
- A string slice containing ASCII encoded bytes.
Returns
- [String] - A
String
containing the ASCII encoded bytes.
Examples
use std::string::String;
fn foo() {
let string = String::from_ascii_str("ABCDEF");
}
fn is_empty(self) -> bool
fn is_empty(self) -> bool
Returns a bool
indicating whether the String
is empty.
Returns
- [bool] -
true
if theString
is empty,false
otherwise.
Examples
use std::string::String;
fn foo() {
let mut string = String::new();
assert(string.is_empty());
string.push(0u8);
assert(!string.is_empty());
}
fn new() -> Self
fn new() -> Self
Constructs a new instance of the String
type.
Returns
- [String] - A new empty instance of the
String
type.
Examples
use std::string::String;
fn foo() {
let string = String::new();
string.push(0u8);
}
fn with_capacity(capacity: u64) -> Self
fn with_capacity(capacity: u64) -> Self
Constructs a new instance of the String
type with the specified capacity.
Arguments
capacity
: [u64] - The specified amount of bytes on the heap to be allocated for theString
.
Returns
- [String] - A new empty instance of the
String
type with the specified capacity.
Examples
use std::string::String;
fn foo() {
let string = String::with_capacity(1);
string.push(0u8); // This will not reallocate
string.push(1u8); // This will reallocate
}
fn ptr(self) -> raw_ptr
fn ptr(self) -> raw_ptr
Gets the pointer of the allocation.
Returns
[raw_ptr] - The location in memory that the allocated string lives.
Examples
fn foo() {
let string = String::new();
assert(!string.ptr().is_null());
}
Trait Implementations
impl AsRawSlice for String
impl AsRawSlice for String
fn as_raw_slice(self) -> raw_slice
fn as_raw_slice(self) -> raw_slice
Returns a raw slice to all of the elements in the string.
impl From<raw_slice> for String
impl From<raw_slice> for String
fn from(slice: raw_slice) -> Self
fn from(slice: raw_slice) -> Self
Converts a raw_slice
to a String
.
Arguments
slice
: [raw_slice] - Theraw_slice
to convert to aString
.
Returns
- [String] - The newly created
String
.
Examples
use std::{alloc::alloc, string::*};
fn foo() {
let ptr = alloc::<u64>(1);
let slice = raw_slice::from_parts::<u64>(ptr, 1);
let string: String = String::from(slice);
}
impl Eq for String
impl Eq for String
fn eq(self, other: Self) -> bool
fn neq(self, other: Self) -> bool
fn neq(self, other: Self) -> bool
Evaluates if two values of the same type are not equal.
Additional Information
This function is inherited when eq()
is implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
if the two values are not equal, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 2 };
let result = struct1 != struct2;
assert(result);
}