continuing ch 3 finished branch section

This commit is contained in:
darkicewolf50 2024-12-28 19:16:18 -07:00
parent f2b1879539
commit 948e623ac6
5 changed files with 46 additions and 2 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
/target /target
/**/target/
/*/target /*/target/

7
branches/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 = "branches"
version = "0.1.0"

6
branches/Cargo.toml Normal file
View File

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

30
branches/src/main.rs Normal file
View File

@ -0,0 +1,30 @@
fn main() {
// comments go above line attempting to comment
println!("Hello, world!");
example_funct();
}
// implicitly implied that a unit type is returned but neat to know it is allowed
fn example_funct () -> () {
let number = 1;
// allowed bool statement
let run_if = number == 0;
// each if is a different arm or branch of the code path, will not check any other files once the first one is true
// must be a bool, rust will not accept if number or etc, must be converted into bool
if number != 0 {
println!("Number is non-zero")
// multiple ifs in a row
} else if run_if {
println!("Number is zero")
// in the ase that all are flase
} else {
println!("Number is not real")
}
let condition = true;
// ifs can also be part of a statement to set a value depending on another value
let another_number = if condition { 7 } else { 0 };
}
// too many ifs or if elses can clutter code, use the match case instead
// 5 ifs should mean the code needs to be refactored

View File

@ -41,7 +41,7 @@ fn example_function (x:i8, y:u16) {
// EXPRESSIONS DO NOT END WITH ; // EXPRESSIONS DO NOT END WITH ;
// if you add a ; to the end of a statment it will turn into a statement and not return anything // if you add a ; to the end of a statment it will turn into a statement and not return anything
// unless it is a return keyword // unless it is a return keyword
// or if it is a marco // or if it is a marco (not required)
fn with_return_plus_one (c: i16) -> i16 { fn with_return_plus_one (c: i16) -> i16 {