前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Rust - 01 - Getting Started

Rust - 01 - Getting Started

作者头像
szhshp
发布2023-01-05 15:55:01
2770
发布2023-01-05 15:55:01
举报
文章被收录于专栏:szhshp 的第四边境中转站

Hello Cargo!

Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.

Command

Desc

cargo new

Create a project

cargo build

Build a project

cargo build --release

Build release ver

cargo run

Run a project

cargo check

build a project without producing a binary to check for errors using cargo check

Common Programming Concepts

Variables And Mutability

Shadowing & Mutable

For same variable name, the former is shadowed by the latter

代码语言:javascript
复制
fn main() {
    let x = 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 is: {x}"); // prints 6
}

Shadowed variable can change its type

代码语言:javascript
复制
fn main() {
    let spaces = "123123123";
    let spaces = spaces.len();  /* type changed */
    println!("{spaces}");
}

For reassign the variable, you must use let keyword

代码语言:javascript
复制
fn main() {
    let x = 5;
    x = x + 1;  // Error, no let keyword on reassign
}

let allows type change, let mut disallows type changes

代码语言:javascript
复制
fn main() {
    let mut spaces = "   ";
    spaces = spaces.len(); /* Error: Disallow type change for mutable */
} 

Data Types

Rust is a statically typed **language, which means that it must know the types of all variables at compile time.

代码语言:javascript
复制
#![allow(unused)]
fn main() {
    /* type annotation (u32) is required */
    let guess: u32 = "42".parse().expect("Not a number!");
}

Rust has two data type subsets: scalar and compound

Scalar Types
  • Integer
  • Integer Literals
  • Floating-Point
  • Numeric
  • Boolean
  • Character
  • Tuple
  • Array
Functions
  • You can define function anywhere(Before/After) within the scope of the caller
  • Parameter type annotation is required
代码语言:javascript
复制
fn main() {
    print_labeled_measurement(5, 'h');
}

fn print_labeled_measurement(value: i32, unit_label: char) {
    println!("The measurement is: {value}{unit_label}");
}

// You can define function anywhere(Before/After) within the scope of the caller
Statements and Expressions
代码语言:javascript
复制
fn main() {
    let x = (let y = 6); // Error: Statement `let y = 6` do not have return value
}
  • Expressions do not include ending semicolons. If you add a semicolon to the end of an expression, you turn it into a statement, and it will then not return a value.
代码语言:javascript
复制
fn main() {
    let y = {
        let x = 3;
        x + 1     // Attention: No semicolon, means this is a expression 	
    };  // returns 4

    println!("The value of y is: {y}");
}

Function with Return Values

代码语言:javascript
复制
fn main() {
    let x = plus_one(5);

    println!("The value of x is: {x}");
}

fn plus_one(x: i32) -> i32 {
    x + 1  // Attention: no semicolon, express with return value
}

Control Flow

if Must have boolean type

代码语言:javascript
复制
fn main() {
    let number = 12;

    if number % 4 == 0 {  // only boolean type
        println!("number is divisible by 4");
    } else if number % 3 == 0 {
        println!("number is divisible by 3");
    } else {
        println!("number is not divisible by 4, 3, or 2");
    }
}

Use if in a let Statement, but the return values must in same type

代码语言:javascript
复制
fn main() {
    let condition = true;

		// Do not work because return values are not in same type
		// let number = if condition { 5 } else { "six" };
    
		let number = if condition { 5 } else { 6 };

    println!("The value of number is: {number}");
}

Traditional loop

代码语言:javascript
复制
fn main() {
    let mut counter = 0;

    let result = loop {
        /* loop in statement */
        counter += 1;

        if counter == 10 {
            break counter * 2;  /* send the break value as return value */
        }
    };

    println!("The result is {result}");
}

While Loop

代码语言:javascript
复制
fn main() {
    let mut number = 3;

    while number != 0 {
        println!("{number}!");

        number -= 1;
    }

    println!("LIFTOFF!!!");
}

for...in loop

代码语言:javascript
复制
fn main() {
    // 1..10  is a Range from 1 to 9
    // 1..=10 is a Range from 1 to 10 
    // (1..=10).rev() is a Range from 10 to 1 
    
    for number in 1..=10 {
        println!("{number}!");
    }
    println!("DONE!!!");
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-12-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Hello Cargo!
  • Common Programming Concepts
    • Variables And Mutability
      • Shadowing & Mutable
    • Data Types
      • Scalar Types
      • Functions
      • Statements and Expressions
    • Function with Return Values
      • Control Flow
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档