mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 11:07:12 -06:00
continuing ch 3 finished branch section
This commit is contained in:
7
branches/Cargo.lock
generated
Normal file
7
branches/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 = "branches"
|
||||
version = "0.1.0"
|
6
branches/Cargo.toml
Normal file
6
branches/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "branches"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
30
branches/src/main.rs
Normal file
30
branches/src/main.rs
Normal 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
|
Reference in New Issue
Block a user