complete functions
This commit is contained in:
@@ -29,5 +29,53 @@ pub fn run() {
|
||||
|
||||
|
||||
println!("\n=== 2.2 compound types ===\n");
|
||||
// Compound type
|
||||
println!("\n=== tuples ===\n");
|
||||
// Tuple type
|
||||
// grouping various multiple values into one type. Primitive compound types:
|
||||
// tuple and array
|
||||
//
|
||||
// Various types can be inside a tuple, and must be specified beforehand as such: PS not true,
|
||||
// data types can be inferred.
|
||||
|
||||
let tup: (i32, f64, u8) = (500, 5.4, 8);
|
||||
|
||||
// This approach is called destructuring, as it destructures a tuple and allows the program to
|
||||
// use them as individual immutable variables
|
||||
let (x, y, z) = tup;
|
||||
|
||||
println!("here is the value of y: {}", y);
|
||||
println!("Here are the values of x and z. x: {x}, z: {z}");
|
||||
|
||||
// Tuples can also be accessed with var name, then a dot (.) and then the index, starting from
|
||||
// 0, as such:
|
||||
|
||||
let tup2: (i32, u8, f64) = (1234, 254, 65.8);
|
||||
|
||||
let index_zero = tup2.0;
|
||||
let index_one = tup2.1;
|
||||
let index_two = tup2.2;
|
||||
|
||||
println!("here are the tuple decustructed indexed values method: index zero: {}, index one: {}, index two: {}", index_zero, index_one, index_two);
|
||||
println!("\n=== arrays ===\n");
|
||||
// Arrays can only contain one type. Arrays in rust have a fixed length.
|
||||
// syntax is as such: let a = [1,2,3,4,5];
|
||||
// arrays live on the stack. only use if you always know the number of items in the array.
|
||||
// Else, chances are you want to use vectors from the std lib. Vectors live in the heap. This is covered later in the book.
|
||||
|
||||
let months = ["January", "February", "March", "April", "May", "June", "July",
|
||||
"August", "September", "October", "November", "December"];
|
||||
|
||||
// you can initialize the array with type and length as such:
|
||||
// let a: [i32;5] = [1,2,3,4,5];
|
||||
|
||||
// you can access array elements as such:
|
||||
|
||||
let third_month = months[2];
|
||||
let fifth_month = months[4];
|
||||
|
||||
println!("now we access the month array by index. The third month is {third_month}. The fifth month is {fifth_month}");
|
||||
|
||||
// Note, the compiler will complain if you try to access a value that is outside the array
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
// we have so far seen the most important function in use, the main function which is the main entry
|
||||
// point of all/most rust programs.
|
||||
// `fn` declares a enew function.
|
||||
|
||||
pub fn run() {
|
||||
println!("\n=== 2.4 functions ===\n");
|
||||
|
||||
another_function();
|
||||
func_with_params(42);
|
||||
print_labeled_measurement(4, 'h');
|
||||
let x = five();
|
||||
println!("the return value of the five funtion is: {x}");
|
||||
let add_one = plus_one(10);
|
||||
println!("The plus one return type: {add_one}");
|
||||
}
|
||||
|
||||
fn another_function() {
|
||||
println!("hello, I come from another_function");
|
||||
}
|
||||
|
||||
// function signatures are REQUIRED to have type specification.
|
||||
fn func_with_params(x: i32) {
|
||||
println!("from function with params, the value of x is: {x}");
|
||||
}
|
||||
|
||||
// func with multiple signatures
|
||||
fn print_labeled_measurement(value: u32, unit_label: char) {
|
||||
println!("from multiple signatures func\nThe measurement is: {value}{unit_label}");
|
||||
}
|
||||
|
||||
// Statements and expressions
|
||||
// Okay, as I understand it, statements are the majority of code we have written so far.
|
||||
// statements end with semi colon (;), and do not return anything.
|
||||
// Expressions do NOT end with (;), but do return something. Look at these examples:
|
||||
|
||||
fn five() -> u32 {
|
||||
5
|
||||
}
|
||||
|
||||
// the -> indicates the return type.
|
||||
// Now, the next function will not compile, because the statement will end with a semi colon, and
|
||||
// thus the required return type will come up empty. I have commented the first line out, and the
|
||||
// second line is valid, as it returns an expression
|
||||
|
||||
fn plus_one(x: u32) -> u32 {
|
||||
// x + 1;
|
||||
x + 1
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
mod variables;
|
||||
mod data_types;
|
||||
mod functions;
|
||||
|
||||
fn main() {
|
||||
variables::run();
|
||||
println!("\n#####################\n");
|
||||
data_types::run();
|
||||
println!("\n#####################\n");
|
||||
functions::run();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user