Aggregate Data Types
Other Aggregate Types
Arrays with size e.g. [int;12]
Vec
Strings
Arrays
Array types include their size
[char; 7]
There are no "const generics": every size is different
Arrays are copy if their elements are
Initialization syntax is either by copyable value
let a = [0u16; 12];
or by list
let a = ['a', 'b', 'c'];
Arrays must always be created initialized.
The intent of arrays is that small ones are useful in numeric computing e.g.
[f32;4]
and large ones are usually via slices
Slices
Slice types are types like
[char]
that denote a sequence of values of unspecified size: the user can't create these directlyTaking the address of an array normally produces a a slice reference
let a: &[char] = &['a', 'b'];
A slice reference is a "fat pointer" that carries both the slice address and slice length.
Both arrays and slices have a
.len()
method: slices have to have it.
Vec
A
Vec
is an owned value that lives on the heap. The size is part of theVec
, and theVec
can be grown and shrunk, often with the.push()
and.pop()
methodsvec.push(val)
pushesval
on the end ofvec
, increasing its length. The usual ownership rules apply:.push()
borrowsvec
mutably, andval
moves intovec
vec.pop()
moves the value on the end ofvec
out as the result, decreasingvec
's length..pop()
returnsOption<T>
: it returnsNone
if thevec
is empty andSome(val)
otherwise
A
Vec
is notCopy
, but it isClone
if its elements areClone
A new empty
Vec
can be created viaVec::new() Vec::with_capacity(15)
Note that that second thing is also empty — it just has reserved space for efficiency
The
vec!
macro can be used to create vectors:vec![1, 2, 3]
A
Vec
reference is normally auto-converted to a slicelet s: &[char] = &vec![1, 2, 3];
String
A
String
is an owned UTF-8 text on the heap. To say that it is a sequence ofchar
s is wrong: the text is compressed according to the UTF-8 standardA
String
is notCopy
but it isClone
An empty
String
can be created withString::new() String::with_capacity(12)
The capacity is in bytes, not chars
.push()
and.pop()
can be used to add or remove characters on the end of aString
Strings are often created using the
.to_string()
method of various other datatypesThe
format!
macro can be used to create stringsprintln!
-style. There is noformatln!
for some reasonformat!("{} bottles of root beer", 99)
String indexing is in bytes because UTF-8, if it were supported, which it isn't. Use the
.chars()
method to iterate over the characters of aString
// This will not compile. for i in 0..s.len() { println!("{}", s[i]); } // Write this instead. for c in s.chars() { println!("{}", c); }
str
str
is a slice of UTF-8 text of unspecified length. There is no obvious way to create a value of this type directly in a Rust program. Instead, this type is used as a slice&str
A string constant is a string slice
let s: &str = "hello world";
A
String
reference normally autorefs to a string slicelet s: &str = &5.to_string();
There's a whole pile of methods for manipulating
&str
: see the docs