Rust Programming :The Complete Developor‘s Guide--01 Fundamentals

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”
Example:Simple Flow
let a = 1;
let b = 2;
let c = 3;
Program Start
a = 1
b=2
c=3
Program End
Example: if…else
let a = 99;
if a > 99 {
	println!("Big number");
}else {
	println!("Small number");
}
Yes
No
Program Start
a = 99
a > 99
print big number
Program End
print 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");
    }
}
Yes
Yes
No
No
Program Start
a = 99
a > 99
a > 200
rint huge number
Program End
rint big number
print 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;
    }
}
Yes
No
Program Start
a = 0
a == 5
Program End
print a
increment a
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!"),
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

血_影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值