User Tools

Site Tools


devlogs:15_5_2022

Sunday, May 15th 2022

k

have you play this game? this. its pretty funny.

Collections

Collections are like arrays or tuples whose size do not need to be known at compile time. The two types are

  1. Vectors
  2. HashMap (like Dictionaries in c#)

Vectors

Vectors are resizable arrays whos length do not need to be known at compile time but the size of its type needs to be known.

You can init a vector with the vec! macro. eg

 let x = vec![20,50,20]; 

You can access a certain element like dish rish this*

 println!("{}",x[0]);  

You can iterate through every value like dish rish this* 1)

stolen from the rust by example book

// `Vector`s can be easily iterated over
println!("Contents of xs:");
for v in x.iter() {
 println!("> {}", x);
}
 
// A `Vector` can also be iterated over while the iteration
// count is enumerated in a separate variable (`i`)
for (i, v) in x.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 v in xs.iter_mut() {
 *v *= 3;
}
println!("Updated vector: {:?}", xs);

HashMap

Like dictionaries these use key value pairs.

To make one

let mut name_and_age = HashMap::new();
 
name_and_age.insert("Gassica",22);
name_and_age.insert("Alex",12);
 
name_and_age.get("Alex") //ACCESSING THE VALUE

You can also insert “entry”

name_and_age.entry(String::from("thomas")).or_insert(20); //Insert only if unique

which is bascially insert if unique. The aims to finish this by may atleast. (given I spend only a little time a week on it) will prollly do some exercizes thru the week without typing my noon-sense.2).

1)
haha, I'm so quirky
2)
but typing this is fun fun fun fun
devlogs/15_5_2022.txt · Last modified: 2023/10/19 15:25 (external edit)