Install
- Linux
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Uninstall
rustup self uninstall
Data Types
- Memory only stores binary data
- Anything can be represented in binary
- Program determines what the binary represents
- Basic types that are universally useful are provided by the language
Basic Data Types
Basic data types are: boolean, integer, double & float, character, string
- Boolean
- true, false
- Integer
- 1, 2, 50, 99, -2
- Double/Float
- 1.1, 5.5, 200.0001, 2.0
- Character(single quotation mark)
- ‘A’, ‘B’,‘c’, ‘6’, ‘$’
- String(double quotes to denote strings)
- “Hello”, “string”, “this is a string”, “it’s 42”
What is a variable
- Assign data to a temporary memory location
- Allows programmer to easily work with memory
- Can be set to any value & type
- Immutable by default, but can be mutable
- Immutable: cannot be changed
- mutable: can be changed
Examples
fn main() {
let two = 2;
let hello = "hello";
let j = 'j';
let my_half = 0.5;
let mut my_name = "Bill";
let quit_program = false;
let your_half = my_half;
}
Recap
- Variables make it easier to work with data
- Variables can be assigned to any value
- This include other variables
- Immutable by default
Functions
What are functions?
- A way to encapsulate program functionality
- Optionally accept/return data
- Utilized for code organization
- Also makes code easier to read
Recap
- Functions encapsulate functionality
- Useful to organize code
- Can be executed by “calling” the function
- Parameters determine what data a function can work with
- Optionally “returns” data
- Data sent back from the function
Parameter
Func Name /|\
/|\ | Return Type
| |------------| |
fn add(a: i32, b: i32) -> i32 {
a + b
}
let x = add(1, 2);
let y = add(3, 0);
let z = add(x, 1);
The println macro
- “Prints”(displays) information to the terminal
fn main() {
let life = 42;
println!("hello");
println!("{:?}", life);
println!("{:?} {:?}", life, life);
}
Control flow using “if”
- Execution Flow: Code executed line-by-line
- Actions are performed & control flow may change
- Specific conditions can change control flow
- “if”
- “else”
- “else if”
- Specific conditions can change control flow
Example:Simple Flow
let a = 1;
let b = 2;
let c = 3;
Example: if…else
let a = 99;
if a > 99 {
println!("Big number");
}else {
println!("Small number");
}
Example: Nested if…else
fn main() {
let a = 99;
if a > 99 {
if a > 200 {
println!("Huge number");
} else {
println!("Big number");
}
} else {
println!("Small number");
}
}
Example: if…else if… else
fn main() {
let a = 99;
if a > 200 {
println!("Huge number");
} else if a > 99 {
println!("Big number");
} else {
println!("Small number");
}
}
Recap
- Code executes line-by-line
- This can be changed using “if”
- Try to always include “else”, unless there truly is no alternative case
Repetition using loops
- Called “looping” or “iteration”
- Multiple types of loops
- “loop” – infinite loop
- “while” – conditional loop
Loop
fn main() {
let mut a = 0;
loop {
if a == 5 {
break;
}
println!("{:?}", a);
a = a + 1;
}
}
while loop
fn main() {
let mut a = 0;
while a != 5 {
println!("{:?}", a);
a = a + 1;
}
}
Recap
- Repetition can be performed using loops
- while loop
- infinite loop
- Both types of loops can exit using “break”
Activity: Functions
// Topic: Functions
//
// Programe requirements
// * Displays your first and last name
//
// Notes:
// * Use a function to display your first name
// * Use a function to display your last name
// * Use the println macro to display messages to the terminal
// * Use a function to display your first name
fn first_name() {
println!("Jayson");
}
// * Use a function to display your last name
fn last_name() {
println!("Lennon");
}
fn main() {
first_name();
last_name();
}
Demo: Basic arithmetic
fn sub(a: i32, b: i32) -> i32 {
a - b
}
fn main() {
let sum = 2 + 2;
let vaule = 10 - 5;
let division = 10 / 2;
let mult = 5 * 5;
let five = sub(8, 3);
let rem = 6 % 3;
let rem2 = 6 % 4;
}
Demo: Basic Math
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn display_result(result: i32) {
println!("{:?}", result);
}
fn main() {
let result = add(2, 2);
display_result(result);
}
Demo: Control flow with if & else
fn main() {
let age = 15;
if age >= 21 {
println!("ok to purchase");
} else {
println!("cannot purchase");
}
}
flow control using if…else if…else
fn main() {
let n = 7;
if n > 5 {
println!(">5");
} else if n < 5 {
println!("<5");
} else {
println!("=5");
}
}
Match
- Add logic to program
- Similar to if…else
- Exhaustive
- All options must be accounted for
fn main() {
let some_bool = true;
match some_bool {
true => println!("its true"),
false => println!("its false"),
}
}
fn main() {
let some_int = 3;
match some_int {
1 => println!("its 1"),
2 => println!("its 2"),
3 => println!("its 3"),
_ => println!("its something else"),
}
}
match vs else…if
- match will be checked by the compiler
- If a new possibiliy is added, you will be notified when this occurs
- else…if is not checked by the compiler
- If a new possibiliy is added,your code may contain a bug
Recap
- Prefer match over else…if when working with a single variable
- match considers all possibilities and more robust code
- Use underscore(_) to match “anything else”
Demo:Making decisions with match
fn main() {
let my_name = "Bob";
match my_name {
"Jayson" => println!("this is my name"),
"Bob" => println!("not my name"),
"Alice" => println!("hello alice"),
_ => println!("nice to meet you!"),
}
}