Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
adding files
  • Loading branch information
fred-sheehan authored Mar 12, 2024
1 parent a6df65d commit 052c82f
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 0 deletions.
7 changes: 7 additions & 0 deletions control_flow/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions control_flow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "control_flow"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
3 changes: 3 additions & 0 deletions control_flow/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
7 changes: 7 additions & 0 deletions functions/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions functions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "functions"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
22 changes: 22 additions & 0 deletions functions/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fn main() {
println!("Hi there, ");

a_called_function(); // we just call this function without any parameters

another_function(5, 6); // we call this function with two parameters

print_label_measurement(5, 'h');
}

fn a_called_function() {
println!("I'm a_called_function!");
}

// in function signatures, we MUST define the type of each parameter value
fn another_function(x: i32, y: i32) {
println!("The value of x: {} and the value of y: {}", x, y);
}

fn print_label_measurement(value: i32, unit_label: char) {
println!("The measurement is: {value} {unit_label}");
}
Empty file added functions/{directory}
Empty file.
75 changes: 75 additions & 0 deletions guessing_game/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions guessing_game/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
4 changes: 4 additions & 0 deletions guessing_game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Taken from the Rust Book

Basic concepts of the language, used to make a very simple number guessing game.
Should be commented enough to be understandable.
34 changes: 34 additions & 0 deletions guessing_game/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main() {
println!("Guess the number!");

let secret_number: u32 = rand::thread_rng().gen_range(1..=100);

loop {
println!("Please input your guess.");

let mut guess = String::new();

io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");

let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};

println!("You guessed: {guess}");

match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {println!("You win!");
break;
}
}
}
}
7 changes: 7 additions & 0 deletions variables/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions variables/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "variables"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
100 changes: 100 additions & 0 deletions variables/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
fn main() {
let x: i32 = 5;

let x = x + 1;

{
let x = x * 2;
println!("The value of x in the inner scope is: {x}");
}

println!("The value of x in the outer scope is: {x}");

let x = 2.0; // will be f64 the arch of CPU - as no explicit type given

let y: f32 = 3.0; // will be f32 as explicit type given

println!("x: {x}, y: {y}");

// addition
let sum = 5 + 10;

// subtraction
let difference = 95.5 - 4.3;

// multiplication
let product = 4 * 30;

// division
let quotient = 56.7 / 32.2;
let truncated = -5 / 3; // will give -1

// remainder, properly called modulo
let remainder = 43 % 5;

// boolean
let tr_ue = true; // with implicit type
let fal_se: bool = false; // with explicit type

// char types are 4 bytes in size and represent a Unicode Scalar Value
let c = 'z';
let z: char = 'ℤ'; // with explicit type
let heart_eyed_cat = '😻';

// printing the values so compiler doesn't complain about unused variables
println!("x: {x}, sum: {sum}, difference: {difference}, product: {product}, quotient: {quotient}, truncated: {truncated}, remainder: {remainder}, tr_ue: {tr_ue}, fal_se: {fal_se}, c: {c}, z: {z}, heart_eyed_cat: {heart_eyed_cat}");

// tuple
let tup: (i32, f64, u8) = (500, 6.4, 1);

let (x, y, z) = tup; // destructuring the tuple

println!("x: {x}, y: {y}, z: {z}");

let x: (i32, f64, u8) = (500, 6.4, 1); // tuples can hold different data types

let five_hundred = x.0; // accessing the first element of the tuple and
let six_point_four = x.1; // the rest, by tuple index number, using a period
let one = x.2; // followed by the index number on the variable

print!("five_hundred: {five_hundred}, six_point_four: {six_point_four}, one: {one}");

// array
let a = [1, 2, 3, 4, 5]; // arrays must contain the same data types

println!("a0: {}, a1: {}, a2: {}, a3: {}, a4: {}",
a[0], a[1], a[2], a[3], a[4]);

// arrays are useful when you want your data allocated on the stack, not
// the heap, and you know the size of the array at compile time
let months: [&str; 12] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

println!("month 1: {}, month 2: {}, month 3: {}, month 4: {}, month 5: {}, month 6: {}, month 7: {}, month 8: {}, month 9: {}, month 10: {}, month 11: {}, month 12: {}", months[0], months[1], months[2], months[3], months[4], months[5], months[6], months[7], months[8], months[9], months[10], months[11]);

// here we are specifying the type and length of the array [i32; 5]
// and then giving it our actual values to store in the array
let a: [i32; 5] = [1, 2, 3, 4, 5];

let b = [3; 5]; // this will create an array that contains 5 elements
// each with the value of 3
println!("b0: {}, b1: {}, b2: {}, b3: {}, b4: {}",
b[0], b[1], b[2], b[3], b[4]);

// just like tuples, we can destructure arrays
let first = a[0]; // accessing the first element of the array [1]
let second = a[1]; // accessing the second element of the array [2]

// both the tuple and the array structures are zero-indexed and (start at 0)
// and the length of the tuple or array is fixed, meaning it cannot grow or
// shrink. If you try and access an index that is out of range, your program
// will panic at runtime!

println!("first: {first}, second: {second}");

let t = ([1; 2], [3; 4]);

let (a, _b) = t;

println!("{}", a[0] + t.1[0]);

}

0 comments on commit 052c82f

Please sign in to comment.