Compare commits

..

6 Commits

Author SHA1 Message Date
Sil Klaasboer
d7d02a4935 add enums and data holding enums 2025-12-18 14:08:01 +01:00
Sil Klaasboer
1d8a7faddf add ownership and borrowing testing 2025-12-18 13:52:08 +01:00
Sil Klaasboer
2069c2d469 add String type 2025-12-18 13:30:06 +01:00
Sil Klaasboer
ca2c8bdcc6 add functions 2025-12-18 13:11:54 +01:00
Sil Klaasboer
7b9aad5fc8 add for loops 2025-12-18 12:58:26 +01:00
Sil Klaasboer
de13a80f99 add loop and loop expressions 2025-12-18 12:50:31 +01:00
8 changed files with 160 additions and 0 deletions

7
containers/Cargo.lock generated Normal file
View 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
View File

@@ -0,0 +1,6 @@
[package]
name = "containers"
version = "0.1.0"
edition = "2024"
[dependencies]

32
containers/src/main.rs Normal file
View 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),
}
}

View File

@@ -42,4 +42,68 @@ fn main() {
}; };
println!("New number for num2: {}",num2); 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
View 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
View File

@@ -0,0 +1,6 @@
[package]
name = "ownership"
version = "0.1.0"
edition = "2024"
[dependencies]

34
ownership/src/main.rs Normal file
View 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);
}

View File

@@ -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";