diff --git a/branches/Cargo.lock b/branches/Cargo.lock new file mode 100644 index 0000000..b420e22 --- /dev/null +++ b/branches/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "branches" +version = "0.1.0" diff --git a/branches/Cargo.toml b/branches/Cargo.toml new file mode 100644 index 0000000..6934aa4 --- /dev/null +++ b/branches/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/branches/src/main.rs b/branches/src/main.rs new file mode 100644 index 0000000..6c9d112 --- /dev/null +++ b/branches/src/main.rs @@ -0,0 +1,46 @@ +fn main() { + let number = 3; + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } + + let mut counter = 0; + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + println!("{result}result"); + + + + let mut count = 0; + 'counting_up: loop { + println!("count = {count}"); + let mut remaining = 10; + loop { + println!("remaining = {remaining}"); + if remaining == 9 { + break; + } + if count == 2 { + break 'counting_up; + } + remaining -= 1; + } + count += 1; + } + println!("{count} count"); + + + // for number in (1..4).rev() { // exclusive + for number in (1..=4).rev() { // inclusive + println!("{number}!"); + } + println!("liftoff"); + +} diff --git a/variables/src/main.rs b/variables/src/main.rs index 7fc83b7..966272a 100644 --- a/variables/src/main.rs +++ b/variables/src/main.rs @@ -1,3 +1,5 @@ +use std::io; + fn main() { let x = 5; let x = x + 1; @@ -15,5 +17,29 @@ fn main() { println!("fy: {fy}"); - // last page 39 + + let a = [1,2,3,4,5]; + let mut index = String::new(); + io::stdin() + .read_line(&mut index) + .expect("failed"); + + let index :usize = index.trim().parse().expect("not a number)"); + let element = a[index]; + println!("{index} is {element}"); + + let mut y = { + let x = 3; + x + 1 + }; + println!("{y}"); + y = five(); + println!("{y}"); + } + + + +fn five () -> i32 { + 5 +} \ No newline at end of file