first commit
This commit is contained in:
commit
a66604eb6c
52 changed files with 790 additions and 0 deletions
7
Control_Flow/Cargo.lock
generated
Normal file
7
Control_Flow/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "Control_Flow"
|
||||
version = "0.1.0"
|
||||
6
Control_Flow/Cargo.toml
Normal file
6
Control_Flow/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "Control_Flow"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
3
Control_Flow/README.md
Normal file
3
Control_Flow/README.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
A non-copy type is a type that can't be duplicated smply by copying its bits.
|
||||
1. when you assign or pass a non-copy type, owenership is moved, not duplicated
|
||||
2. After a move, you can no longer use the original valu, unless you explicity clone it back
|
||||
60
Control_Flow/src/main.rs
Normal file
60
Control_Flow/src/main.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
fn main() {
|
||||
/*
|
||||
In rust, Control flow is how your program makes decisions and executes code conditionally or repeatedly.
|
||||
*/
|
||||
|
||||
//1. If Expression
|
||||
println!("if expression");
|
||||
let mut num = 5;
|
||||
if num < 15 {
|
||||
println!("the {} is lesser than 15", num);
|
||||
} else if num == 5 {
|
||||
println!("the {} is 5", num);
|
||||
} else {
|
||||
println!("the number is prolly greater than 15")
|
||||
}
|
||||
|
||||
//2. match expression => generally its like a switch in other language, but more powerful and exhaustive
|
||||
println!("match expression");
|
||||
match num {
|
||||
1 => println!("one"),
|
||||
2 | 3 => println!("two or three"),
|
||||
5 => println!("five"),
|
||||
4..=10 => println!("between 4 and 10"), //from start to end-1 but if =, then end is given there
|
||||
_ => println!("Something else"), //default
|
||||
}
|
||||
|
||||
//3.loop => infinite, can break manually
|
||||
println!("looping");
|
||||
loop {
|
||||
num += 1;
|
||||
if num == 10 {
|
||||
break;
|
||||
}
|
||||
println!("loop count: {}", num);
|
||||
}
|
||||
|
||||
// 4. while loop -> loops while the conditions is true only
|
||||
println!("while loop");
|
||||
while num != 0 {
|
||||
println!("{}", num);
|
||||
num -= 1;
|
||||
}
|
||||
|
||||
//for looping
|
||||
println!("for looping");
|
||||
let arr = [3, 4, 5, 6, 1, 2, 34, 6, 78];
|
||||
for i in arr.iter() {
|
||||
println!("{}", i);
|
||||
}
|
||||
|
||||
for i in 0..arr.len() {
|
||||
println!("{}", arr[i]);
|
||||
}
|
||||
|
||||
/*
|
||||
using the array or arra.iter() depends, the use of iter is just for read, that can be reused
|
||||
and doesnt consumes array and is the refrence actually, but without it you have full control
|
||||
and is completely reverse of the iter
|
||||
*/
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue