Compare commits
6 Commits
cfc4436eeb
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7d02a4935 | ||
|
|
1d8a7faddf | ||
|
|
2069c2d469 | ||
|
|
ca2c8bdcc6 | ||
|
|
7b9aad5fc8 | ||
|
|
de13a80f99 |
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
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