基础概念

作者: sweetBoy_9126 | 来源:发表于2023-03-05 14:28 被阅读0次

变量

rust 中变量默认是不可变的

fn main() {
    let mut x = 5;
    println!("The value of x is: {x}");
    x = 6;
    println!("The value of x is: {x}");
}
$ cargo run
   Compiling variables v0.1.0 (file:///projects/variables)
error[E0384]: cannot assign twice to immutable variable `x`
 --> src/main.rs:4:5
  |
2 |     let x = 5;
  |         -
  |         |
  |         first assignment to `x`
  |         help: consider making this binding mutable: `mut x`
3 |     println!("The value of x is: {x}");
4 |     x = 6;
  |     ^^^^^ cannot assign twice to immutable variable

For more information about this error, try `rustc --explain E0384`.
error: could not compile `variables` due to previous error

想要可变要加 mut

let mut x = 5;
x = 6

常量

不允许对常量使用 mut。常量不光默认不可变,它总是不可变。声明常量使用 const 关键字而不是 let,并且 必须 注明值的类型。

const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;

隐藏

可以用相同变量名称来隐藏一个变量,以及重复使用 let 关键字来多次隐藏

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}");
}

隐藏与将变量 mut 的区别:
1). 隐藏必须使用 let 关键字
2). 当再次使用 let 时,实际上创建了一个新变量,我们可以改变值的类型,并且复用这个名字,而 mut 不允许我们修改值的类型

元组

用包含在圆括号中的逗号分隔的值列表来创建一个元组。元组中的每一个位置都有一个类型,而且这些不同值的类型也不必是相同的。这个例子中使用了可选的类型注解:

fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);
}
  • 解构
fn main() {
    let tup = (500, 6.4, 1);

    let (x, y, z) = tup;

    println!("The value of y is: {y}");
}
  • 索引访问
    我们也可以使用点号(.)后跟值的索引来直接访问它们。例如:
fn main() {
    let x: (i32, f64, u8) = (500, 6.4, 1);

    let five_hundred = x.0;

    let six_point_four = x.1;

    let one = x.2;
}

数组

与元组不同,数组中的每个元素的类型必须相同,长度是固定的

  • 编写数组类型
    在方括号中包含每个元素的类型,后跟分号,再后跟数组元素的数量。
let a: [i32; 5] = [1, 2, 3, 4, 5];

还可以通过在方括号中指定初始值加分号再加元素个数的方式来创建一个每个元素都为相同值的数组:

let a = [3; 5];
// 等价于
let a = [3, 3, 3, 3, 3];
  • 访问数组
fn main() {
    let a = [1, 2, 3, 4, 5];

    let first = a[0]; // 1
    let second = a[1]; // 2
}

函数

所有字母都是小写并使用下划线分隔单词

fn main() {
    println!("Hello, world!");

    another_function();
}

fn another_function() {
    println!("Another function.");
}
  • 参数
    在函数签名中,必须 声明每个参数的类型
fn main() {
    another_function(5);
}

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

fn main() {
    print_labeled_measurement(5, 'h');
}

fn print_labeled_measurement(value: i32, unit_label: char) {
    println!("The measurement is: {value}{unit_label}");
}
  • 具有返回值的函数
    在箭头(->)后声明它的类型。在 Rust 中,函数的返回值等同于函数体最后一个表达式的值。
fn five() -> i32 {
    5
}

fn main() {
    let x = five();

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

if 表达式

if 里的条件判断必须是 bool 值

fn main() {
    let number = 3;

    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
}
  • 在 let 语句中使用 if
fn main() {
    let condition = true;
    let number = if condition { 5 } else { 6 };

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

loop 循环

可以使用 break 跳出循环

fn main() {
    let mut counter = 0;

    let result = loop {
        counter += 1;

        if counter == 10 {
            break counter * 2;
        }
    };

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

while 循环

fn main() {
    let mut number = 3;

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

        number -= 1;
    }

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

相关文章

  • Golang Gist

    概念讲解 基础概念 基础问答 基础问答 编码能力考查x

  • 基础概念

    Ecmascript 是一个脚本语言标准,规定这个语言的语法,内置函数等等 Javascript是Ecmascri...

  • 基础概念

    集合竞价视频 做市做市商 交易时间 第一次集合竞价时间:9:15-9:25; 做市商做市前报价时间:9:25-9:...

  • 基础概念

    一:统计学和机器学习的比较和练习: 二: from:https://ai.baidu.com/paddlepadd...

  • 基础概念

    净流动资产值 格雷厄姆的主要选股规则, 是买入市值低于净流动资产值 2/3 的企业,待股价超过净资产以上某比例后卖...

  • 基础概念

    1.基本概念 Java是纯面向对象语言,“Everything is Object”;所有代码(包括函数、变量等)...

  • 基础概念

    参考教程,廖雪峰 数据类型 Number js不区分整数合浮点数,统一使用Number。NaN:表示Not a N...

  • 基础概念

    关键 标识符 所谓标识符是指变量、函数、属性的名字,或者函数的参数。标识符的书写有几个特征 区分大小写 第一个字符...

  • 基础概念

    编译后的二进制代码才是机器语言,或者叫机器代码。汇编语言亦称为符号语言。编译器将源程序转换成机器能理解的程序。无符...

  • 基础概念

    基于内容 基于用户的协同过滤 基于商品的协同过滤

网友评论

    本文标题:基础概念

    本文链接:https://www.haomeiwen.com/subject/niguldtx.html