finished chaper 3

This commit is contained in:
Tyrel Souza 2023-02-26 00:01:40 -05:00
parent 01ce17da11
commit 54b7b3e4f8
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
4 changed files with 88 additions and 1 deletions

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 = 3
[[package]]
name = "branches"
version = "0.1.0"

8
branches/Cargo.toml Normal file
View File

@ -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]

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

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

View File

@ -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
}