Useful types
- Box<T> - A pointer type that uniquely owns a heap allocation of type T.
- Vec<T> - A contiguous growable array type with heap-allocated contents.
- slice - A reference to a sub-range of a sequence, like a string or a vector.
- String and str
Box<T>
Rust provides a construct called Box<T> for putting data on the heap. For example, we can wrap the million-element array in Box::new like this:
fn main() {
let a = Box::new([0; 1_000_000]);
let b = a;
}
Boxes provide ownership for this allocation, and drop their contents when they go out of scope. Boxes also ensure that they never allocate more than isize::MAX bytes.
Related tops:
- raw pointers:
Box::<T>::from_rawandBox::<T>::into_raw T: Sizedis crucial for ABI-compatible with C pointers- smart pointers
- memory layout
Examples that using raw pointers:
- enqueue() and dequeue() methods of Queue
Slices
Slices are a special kind of reference that refer to sub-ranges of a sequence, like a string or a vector. At runtime, a slice is represented as a “fat pointe” which contains a pointer to the beginning of the range and a length of the range.
Here is what a string slice looks like: string slice.
Examples:
fn main() {
// slice of a string - string slice
let s = String::from("hello world");
let slice: &str = &s[0..5];
assert_eq!(slice, "hello");
// slice of an array
let mut a = [1, 2, 3, 4, 5];
let slice = &mut a[1..3];
slice[0] = 9;
assert_eq!(slice, &[9, 3]);
}
String and str
(For the memory layout, please refer to string slice).
The String type is a growable, mutable, owned, UTF-8 encoded string type. It is provided by Rust’s standard library rather tha coded into the core language.
The string slice str, usually seen in its borrowed form &str, is a reference to some UTF-8 encoded string data stored elsewhere. It’s the only one string type in the core language.
- Both String and string slices are UTF-8 encoded
- String type does not support direct indexing
- String type does support iterating through