Выбрать главу

println!("Boxed rectangle occupies {} bytes on the stack",

mem::size_of_val(&boxed_rectangle));

println!("Boxed box occupies {} bytes on the stack",

mem::size_of_val(&box_in_a_box));

// Copy the data contained in `boxed_point` into `unboxed_point`

let unboxed_point: Point = *boxed_point;

println!("Unboxed point occupies {} bytes on the stack",

mem::size_of_val(&unboxed_point));

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Vectors are re-sizable arrays. Like slices, their size is not known at compile time, but they can grow or shrink at any time. A vector is represented using 3 parameters:

   • pointer to the data

   • length

   • capacity

The capacity indicates how much memory is reserved for the vector. The vector can grow as long as the length is smaller than the capacity. When this threshold needs to be surpassed, the vector is reallocated with a larger capacity.

fn main() {

// Iterators can be collected into vectors

let collected_iterator: Vec<i32> = (0..10).collect();

println!("Collected (0..10) into: {:?}", collected_iterator);

// The `vec!` macro can be used to initialize a vector

let mut xs = vec![1i32, 2, 3];

println!("Initial vector: {:?}", xs);

// Insert new element at the end of the vector

println!("Push 4 into the vector");

xs.push(4);

println!("Vector: {:?}", xs);

// Error! Immutable vectors can't grow

collected_iterator.push(0);

// FIXME ^ Comment out this line

// The `len` method yields the number of elements currently stored in a vector

println!("Vector length: {}", xs.len());

// Indexing is done using the square brackets (indexing starts at 0)

println!("Second element: {}", xs[1]);

// `pop` removes the last element from the vector and returns it

println!("Pop last element: {:?}", xs.pop());

// Out of bounds indexing yields a panic

println!("Fourth element: {}", xs[3]);

// FIXME ^ Comment out this line

// `Vector`s can be easily iterated over

println!("Contents of xs:");

for x in xs.iter() {

println!("> {}", x);

}

// A `Vector` can also be iterated over while the iteration

// count is enumerated in a separate variable (`i`)

for (i, x) in xs.iter().enumerate() {

println!("In position {} we have value {}", i, x);

}

// Thanks to `iter_mut`, mutable `Vector`s can also be iterated

// over in a way that allows modifying each value

for x in xs.iter_mut() {

*x *= 3;

}

println!("Updated vector: {:?}", xs);

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

More Vec methods can be found under the std::vec module

There are two types of strings in Rust: String and &str.

A String is stored as a vector of bytes (Vec<u8>), but guaranteed to always be a valid UTF-8 sequence. String is heap allocated, growable and not null terminated.

&str is a slice (&[u8]) that always points to a valid UTF-8 sequence, and can be used to view into a String, just like &[T] is a view into Vec<T>.

fn main() {

// (all the type annotations are superfluous)

// A reference to a string allocated in read only memory

let pangram: &'static str = "the quick brown fox jumps over the lazy dog";

println!("Pangram: {}", pangram);

// Iterate over words in reverse, no new string is allocated

println!("Words in reverse");

for word in pangram.split_whitespace().rev() {

println!("> {}", word);

}

// Copy chars into a vector, sort and remove duplicates

let mut chars: Vec<char> = pangram.chars().collect();

chars.sort();

chars.dedup();

// Create an empty and growable `String`

let mut string = String::new();

for c in chars {

// Insert a char at the end of string

string.push(c);

// Insert a string at the end of string

string.push_str(", ");

}

// The trimmed string is a slice to the original string, hence no new

// allocation is performed

let chars_to_trim: &[char] = &[' ', ','];

let trimmed_str: &str = string.trim_matches(chars_to_trim);

println!("Used characters: {}", trimmed_str);

// Heap allocate a string

let alice = String::from("I like dogs");

// Allocate new memory and store the modified string there

let bob: String = alice.replace("dog", "cat");

println!("Alice says: {}", alice);

println!("Bob says: {}", bob);

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה