mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 02:57:13 -06:00
finished ch3
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
looping_through_a_list();
|
||||
looping_through_a_list_with_rev();
|
||||
}
|
||||
|
||||
// rust has 3 types of loops: loop, while and for
|
||||
@ -72,9 +73,37 @@ fn looping_through_a_list () {
|
||||
let mut index = 0;
|
||||
// if a.len was a static number this would/could cause the program to panic
|
||||
// because if a was modified and the static number was not then it would attempt to go out out the assigned memory
|
||||
// compiler will add code to check if static number is within the bounds and therefore is slower
|
||||
// using list.len() is always a better solution
|
||||
while index < a.len() {
|
||||
println!("the value is: {}", a[index]);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// for loops are the most safe and easy to use
|
||||
|
||||
fn looping_through_a_list_more_concise_and_secure () {
|
||||
let b = [32, 5, 6, 79, 88, 99];
|
||||
// is is more safe and is faster than even a.len() no operation for checking the size everytime/re-eval the bool
|
||||
// removed chance of bug in static number and is faster
|
||||
for element in b {
|
||||
println!("the value is {}", element);
|
||||
}
|
||||
// Ranges are start..end not Range(5), that range method/funct is fake
|
||||
for num in 0..5 {
|
||||
println!("the value is now: {}", num);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn looping_through_a_list_with_rev () {
|
||||
// this reverse the order of a range
|
||||
for number in (1..4).rev() {
|
||||
println!("{}!", number);
|
||||
}
|
||||
|
||||
println!("Lift-Off!!");
|
||||
}
|
||||
|
Reference in New Issue
Block a user