finish ch3.3

This commit is contained in:
Rowan Torbitzky-Lane 2025-03-19 01:43:10 -05:00
parent 5e9e4b1d5e
commit 2cf1980d7d
2 changed files with 173 additions and 1 deletions

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

View File

@ -1,3 +1,168 @@
// const can be used in the global scope.
const THREE_HOURS_IN_SECS: u32 = 60 * 60 * 3;
fn main() {
println!("Hello, world!");
// From ch3.1
// this code wont compile.
// let x = 5;
// println!("The value of x is: {x}");
// // x = 6; // This line is illegal. Can't reassign a non-mutable variable.
// println!("The value of x is: {x}");
//------------------------------------------------------------------------
// Let can only be used inside of a function.
// This makes sense due to the memory freeing paradigm.
let mut y = 5;
println!("The value of y is: {y}"); // Don't need python f string for this!
y = 6;
println!("The value of y is: {y}");
//------------------------------------------------------------------------
let x = 5;
let x = x + 1;
{
let x = x * 2;
println!("The value of x in inner scope is: {x}");
}
println!("The value of x is: {x}");
//------------------------------------------------------------------------
// This compiles
let spaces = " ";
let spaces = spaces.len();
println!("spaces: {spaces}");
// This doesn't
// let mut spacesi = " ";
// spacesi = spacesi.len();
//------------------------------------------------------------------------
// From ch3.2
// Different ways to declare an integer
let decimal_num: isize = 98_222;
let hex_num: isize = 0xff;
let octal_num: isize = 0o77;
let binary_num: isize = 0b1111_0000;
let byte_num: u8 = b'A';
println!("decimal num: {decimal_num}");
println!("hex num: {hex_num}");
println!("octal num: {octal_num}");
println!("binary num: {binary_num}");
println!("byte num: {byte_num}");
//------------------------------------------------------------------------
// floating point types
let a = 2.0;
let b: f32 = 3.0;
//------------------------------------------------------------------------
// boolean types
let t = true;
let f: bool = false;
//------------------------------------------------------------------------
// char types
let c = 'z';
let z: char = '';
let heart_eyed_cat = '😻';
//------------------------------------------------------------------------
// tuples :)
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (i, j, k) = tup;
println!("The value of j is: {j}");
let five_hundred = tup.0;
let six_point_four = tup.1;
let one = tup.2;
let unit = (); // This is essentially rust's null. Similar to Haskell's version.
let mut mut_tup: (i32, i32) = (1, 2);
mut_tup.0 = 0;
mut_tup.1 += 5;
println!("mut_tup: ({}, {})", mut_tup.0, mut_tup.1);
//------------------------------------------------------------------------
// Arrays, rust arrays have fixed length.
// Best when want data allocated on stack rather than heap.
// Vector type is more flexible.
let arr = [1, 2, 3, 4, 5];
// array of same values repeated an amount of times. Functional roots pog
let arr = [3; 5];
// [type; number of elements]
let arr: [i32; 5] = [1, 2, 3, 4, 5];
let first = arr[0];
let second = arr[1];
// Rust will throw an indexing runtime error after it compilies.
// let tenth = arr[10];
//------------------------------------------------------------------------
// ch3.3
another_function(3);
print_labeled_measurement(10, 'c');
//------------------------------------------------------------------------
// This errors. Statements can't return values.
// let x = (let y = 6);
//------------------------------------------------------------------------
// The {...} below is an expression. The last line in the expression has
// no semicolon. If there is a semicolon, it turns the line into a statement
// and thus won't return a value.
let t = {
let x = 3;
x + 1
};
println!("Value of t is: {t}");
//------------------------------------------------------------------------
let num = plus_one(five());
println!("The value of num is: {num}");
}
// Statements: Instructions that perform some action and don't return anything
// like the statements inside of these functions. The function definitions themselves
// are also statements.
// Expressions: Evaluate to a resultant value.
// Sounds like Haskell.
// all functions with parameters must have a type attached to it.
fn another_function(x: i32) {
// ch3.3
println!("Another function.");
println!("The value of x is: {x}");
}
fn print_labeled_measurement(value: i32, unit_label: char) {
println!("The measurement is: {value}{unit_label}");
}
// literally returns 5 with the arrow. Very functional
// Rust. I love this language.
fn five() -> i32 {
5
}
fn plus_one(x: i32) -> i32 {
x + 1
}