Compare commits
8 Commits
1529443824
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7d02a4935 | ||
|
|
1d8a7faddf | ||
|
|
2069c2d469 | ||
|
|
ca2c8bdcc6 | ||
|
|
7b9aad5fc8 | ||
|
|
de13a80f99 | ||
|
|
cfc4436eeb | ||
|
|
7b2a7eeb49 |
7
containers/Cargo.lock
generated
Normal file
7
containers/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 = "containers"
|
||||||
|
version = "0.1.0"
|
||||||
6
containers/Cargo.toml
Normal file
6
containers/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "containers"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
32
containers/src/main.rs
Normal file
32
containers/src/main.rs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
enum Direction
|
||||||
|
{
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Status
|
||||||
|
{
|
||||||
|
Succeeded(String),
|
||||||
|
Failed(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// normal Enum
|
||||||
|
let direction = Direction::Down;
|
||||||
|
match direction {
|
||||||
|
Direction::Up => println!("Going up"),
|
||||||
|
Direction::Down => println!("Going down"),
|
||||||
|
Direction::Left => println!("Going left"),
|
||||||
|
Direction::Right => println!("Going right"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// enum holding data
|
||||||
|
let status = Status::Failed(String::from("Authentication Failed"));
|
||||||
|
match status
|
||||||
|
{
|
||||||
|
Status::Succeeded(message) => println!("Succeeded with msg: {}",message),
|
||||||
|
Status::Failed(message) => println!("Failed with msg: {}",message),
|
||||||
|
}
|
||||||
|
}
|
||||||
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]
|
||||||
109
control_flow/src/main.rs
Normal file
109
control_flow/src/main.rs
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
fn main() {
|
||||||
|
//if else statements
|
||||||
|
let mut num1 = 10;
|
||||||
|
let mut num2 = 5;
|
||||||
|
|
||||||
|
if num1 < num2 {
|
||||||
|
println!("{} is smaller then {}", num1, num2);
|
||||||
|
} else {
|
||||||
|
println!("{} is bigger then {}", num1, num2);
|
||||||
|
}
|
||||||
|
|
||||||
|
//if expressions
|
||||||
|
//always need a else case and must be of the same type, ternary operations do not exist but this results the same
|
||||||
|
num1 = if num1 != num2 { 5 } else { 15 };
|
||||||
|
println!("num 1: {}", num1);
|
||||||
|
|
||||||
|
//match
|
||||||
|
//same as switch case
|
||||||
|
match num1 {
|
||||||
|
1 => println!("Number is 1"),
|
||||||
|
2 => println!("Number is 2"),
|
||||||
|
3 => println!("Number is 3"),
|
||||||
|
6 => println!("Number is 6"),
|
||||||
|
7 => println!("Number is 7"),
|
||||||
|
_ => println!("Number is none of the other statements"),
|
||||||
|
}
|
||||||
|
// _ is a default and altough syntax specifies => it should be an exact match
|
||||||
|
|
||||||
|
//combining match results
|
||||||
|
match num2 {
|
||||||
|
1 | 2 | 3 | 4 => println!("Number is in range of 1 to 4"),
|
||||||
|
5 | 7 => println!("Number is 5 or 7"),
|
||||||
|
_ => println!("Number is none of the above"),
|
||||||
|
}
|
||||||
|
|
||||||
|
//match expression
|
||||||
|
println!("Old number for num2: {}",num2);
|
||||||
|
let num2 = match num2 {
|
||||||
|
1 | 2 | 3 | 4 => 1234,
|
||||||
|
5 | 7 => 57,
|
||||||
|
_ => -1,
|
||||||
|
};
|
||||||
|
println!("New number for num2: {}",num2);
|
||||||
|
|
||||||
|
//loops
|
||||||
|
let mut loop_cycles = 1;
|
||||||
|
loop {
|
||||||
|
|
||||||
|
println!("this is loop: {}",loop_cycles);
|
||||||
|
if loop_cycles == 3 {break};
|
||||||
|
loop_cycles += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
//loop expression
|
||||||
|
loop_cycles = 1;
|
||||||
|
let result = loop {
|
||||||
|
|
||||||
|
println!("cycle: {}",loop_cycles);
|
||||||
|
|
||||||
|
if 7 % loop_cycles == 2 {break loop_cycles};
|
||||||
|
|
||||||
|
loop_cycles += 1
|
||||||
|
};
|
||||||
|
println!("it took {} cycles to resolve the expression",result);
|
||||||
|
|
||||||
|
//while
|
||||||
|
loop_cycles = 1;
|
||||||
|
while 5 > loop_cycles {
|
||||||
|
if loop_cycles == 2
|
||||||
|
{
|
||||||
|
loop_cycles +=1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
println!("while loop {}",loop_cycles);
|
||||||
|
loop_cycles += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//for loops
|
||||||
|
//1..5 is 1 up to 5 not including 5
|
||||||
|
for x in 1..5
|
||||||
|
{
|
||||||
|
println!("for loop: {}",x);
|
||||||
|
}
|
||||||
|
//1..=5 is 1 up to and including 5
|
||||||
|
for x in 1..=5
|
||||||
|
{
|
||||||
|
println!("for loop including: {}",x);
|
||||||
|
}
|
||||||
|
|
||||||
|
//functions
|
||||||
|
custom_func();
|
||||||
|
say_name("John Cena");
|
||||||
|
println!("{}", sum(1,5));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn custom_func()
|
||||||
|
{
|
||||||
|
println!("custom func");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn say_name(name: &str)
|
||||||
|
{
|
||||||
|
println!("my name is: {}",name);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sum(number1 : i32, number2 : i32) -> i32
|
||||||
|
{
|
||||||
|
return number1 + number2;a
|
||||||
|
}
|
||||||
7
ownership/Cargo.lock
generated
Normal file
7
ownership/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 = "ownership"
|
||||||
|
version = "0.1.0"
|
||||||
6
ownership/Cargo.toml
Normal file
6
ownership/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "ownership"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
34
ownership/src/main.rs
Normal file
34
ownership/src/main.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
fn main() {
|
||||||
|
let a ="Hello".to_string();
|
||||||
|
let b = a;
|
||||||
|
|
||||||
|
//println!("{}",a); This will error because a no longer owns the string
|
||||||
|
println!("{}",b);
|
||||||
|
|
||||||
|
//simple types like number characters and booleans are copied not moved
|
||||||
|
let c = 5;
|
||||||
|
let d = c;
|
||||||
|
println!("{}",c);
|
||||||
|
println!("{}",d);
|
||||||
|
|
||||||
|
// when ownership does not need to be moved clone can be used.
|
||||||
|
let e ="Test".to_string();
|
||||||
|
let f = e.clone();
|
||||||
|
|
||||||
|
println!("{}",e);
|
||||||
|
println!("{}",f);
|
||||||
|
|
||||||
|
// references in cpp is called borrowing in rust
|
||||||
|
let g = String::from("Reference test");
|
||||||
|
let h = &g;
|
||||||
|
|
||||||
|
println!("{}", g);
|
||||||
|
println!("{}", h);
|
||||||
|
|
||||||
|
// mutable references
|
||||||
|
let mut i = String::from("Mutable Reference test");
|
||||||
|
let j = &mut i;
|
||||||
|
j.push('!');
|
||||||
|
|
||||||
|
println!("{}", j);
|
||||||
|
}
|
||||||
@@ -11,6 +11,10 @@ fn main() {
|
|||||||
let height: f64 = 1.79;
|
let height: f64 = 1.79;
|
||||||
let character: char = 'M';
|
let character: char = 'M';
|
||||||
let has_driver_license: bool = true;
|
let has_driver_license: bool = true;
|
||||||
|
let mut text = "Hello".to_string();
|
||||||
|
text.push_str(" World");
|
||||||
|
let text2 = String::from("Hello world2");
|
||||||
|
println!("{} {}",text,text2);
|
||||||
|
|
||||||
//make them mutable by mut modifier
|
//make them mutable by mut modifier
|
||||||
let mut middle_name = "Levy";
|
let mut middle_name = "Levy";
|
||||||
|
|||||||
Reference in New Issue
Block a user