feat: ✨ vectors
This commit is contained in:
parent
1cc4d47dab
commit
deda0d8532
2 changed files with 60 additions and 0 deletions
6
21-vectors/Cargo.toml
Normal file
6
21-vectors/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "vectors"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
54
21-vectors/src/main.rs
Normal file
54
21-vectors/src/main.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
enum Types {
|
||||
Number(i32),
|
||||
String(String),
|
||||
Float(f32),
|
||||
}
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
// Actually no need to specify type but I did
|
||||
let mut numbers: Vec<i32> = Vec::new();
|
||||
|
||||
// This adds element to the end of vector
|
||||
numbers.push(0);
|
||||
numbers.push(1);
|
||||
|
||||
for _ in 0..3 {
|
||||
// This tries to remove last element from vector
|
||||
match numbers.pop() {
|
||||
Some(popped) => println!("{}", popped),
|
||||
None => eprintln!("Error: Vector is Empty"),
|
||||
}
|
||||
}
|
||||
|
||||
// This is macro for vector
|
||||
let texts = vec!["Hello", "My", "Friend"];
|
||||
|
||||
// Iteration over vector
|
||||
for text in texts {
|
||||
println!("{}", text);
|
||||
}
|
||||
|
||||
// This won't work because of borrow checker
|
||||
// it prevents us to modify vector in iteration.
|
||||
|
||||
// let mut texts = vec!["Hello", "My", "Friend"];
|
||||
// for text in texts {
|
||||
// texts.push("Let me try");
|
||||
// }
|
||||
|
||||
// This may seem like keeping different types
|
||||
// actually they are belongs to same enum
|
||||
|
||||
let mut different_types = vec![];
|
||||
different_types.push(Types::String(String::from("Hi!")));
|
||||
different_types.push(Types::Number(1));
|
||||
different_types.push(Types::Float(1.1));
|
||||
|
||||
for element in &different_types {
|
||||
match element {
|
||||
Types::Number(number) => println!("{}", number),
|
||||
Types::String(string) => println!("{}", string),
|
||||
Types::Float(float) => println!("{}", float),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue