• Rust冒泡排序


    Rust冒泡排序

    这段代码定义了一个名为 bubble_sort 的函数,接受一个可变的整数类型数组作为输入,然后使用嵌套的循环来实现冒泡排序。外部循环从数组的第一个元素开始迭代到倒数第二个元素,内部循环从数组的第一个元素开始迭代到倒数第二个元素。如果当前元素比下一个元素大,则交换它们的位置。在循环结束后,数组就会按升序排列。

    在 main 函数中,我们定义了一个整数类型的数组,并将其传递给 bubble_sort 函数进行排序。最后,程序将输出排序后的数组

    1. fn bubble_sort(arr: &mut [i32]) {//参数是可变化的i32的数组
    2. let n = arr.len(); //获取arr的数组的长度,坐标从0开始
    3. for i in 0..n { //总循环是n次
    4. for j in 0..n-i-1 { //冒泡排序,每次处理一个减少一次。
    5. if arr[j] > arr[j+1] { //如果 前面的记录后面一个数值,交换,大值冒泡到后面
    6. arr.swap(j, j+1);
    7. }
    8. }
    9. }
    10. }
    11. fn main() {
    12. let mut arr = [5, 2, 9, 1, 5, 6,13,20,18];
    13. bubble_sort(&mut arr);
    14. println!("排序后的数组:{:?}", arr); // 输出 [1, 2, 5, 5, 6, 9]
    15. }

    VsCode 安装Rust插件

    检查运行环境

    编写第一个代码,大家都会,但也要编写,目的是检测环境,安装了rust为1.72.1版本

    1. rustc --version
    2. rustc 1.72.1 (d5c2e9c34 2023-09-13)

    运行基本的代码Hello_world代码,这个简单

    代码为

    1. fn main(){
    2. let s="Hello world!";
    3. println!("{}",s);
    4. }

    let为定义了一个s为字符串,赋值为Hello world!的字符串,fn为函数function的简写,main函数说明是函数的入口,类似C++和c语言,记住基本格式,打印println后面多了一个“!”,其他的类似c++的函数语言,后面用;进行语言结束。

    没有意外是返回Hello world字符串

    调用基本函数

    1. fn Hello_world() ->&static str {
    2. let s="Hello world by keny!";
    3. return s;
    4. }
    5. fn main(){
    6. let Hello_world_str=Hello_world();
    7. println!("{}",Hello_world_str);
    8. }

    还是Hello world的函数。定义了一个Hello_world的函数,函数直接返回字符串,通过main的函数进行调用这个定义的函数,运行结果是Hello world by keny

    1. warning: function `Hello_world` should have a snake case name
    2. --> src/main.rs:1:4
    3. |
    4. 1 | fn Hello_world() ->&'static str {
    5. | ^^^^^^^^^^^ help: convert the identifier to snake case: `hello_world`
    6. |
    7. = note: `#[warn(non_snake_case)]` on by default
    8. warning: variable `Hello_world_str` should have a snake case name
    9. --> src/main.rs:8:9
    10. |
    11. 8 | let Hello_world_str=Hello_world();
    12. | ^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `hello_world_str`
    13. warning: `hello_world` (bin "hello_world") generated 2 warnings
    14. Finished dev [unoptimized + debuginfo] target(s) in 0.69s
    15. Running `target/debug/hello_world`
    16. Hello world by keny!

    Rust语法基础知识

    变量

    首先必须说明,Rust 是强类型语言,但具有自动判断变量类型的能力。这很容易让人与弱类型语言产生混淆。

    如果要声明变量,需要使用 let 关键字。例如:

    let a="abc";

    不允许这样定义,说明a已经定义了字符串,后面有定义了float类型和int类型,实际是冲突的定义

    1. a = "abc";
    2. a = 4.56;
    3. a = 456;

    检测的时候会提示

    1. warning: unused variable: `a`
    2. --> src/main.rs:6:9
    3. |
    4. 6 | let a=123.5;
    5. | ^ help: if this is intentional, prefix it with an underscore: `_a`
    6. warning: unused variable: `a`
    7. --> src/main.rs:8:9
    8. |
    9. 8 | let a=123;
    10. | ^ help: if this is intentional, prefix it with an underscore: `_a`
    11. warning: unused variable: `a`
    12. --> src/main.rs:10:9
    13. |
    14. 10 | let a=true;
    15. | ^ help: if this is intentional, prefix it with an underscore: `_a`

    当然,使变量变得"可变"(mutable)只需一个 mut 关键字。

    1. let mut a=234;
    2. a=567;

    如果这样定义

    1. let a=234
    2. let a=456;
    3. 会编译警告;“`#[warn(unused_variables)]` on by default”

    重影(Shadowing)

    1. let a = 2;
    2. let a=a+1;
    3. let a=a*a;
    4. let a=a*a*a;
    5. println!("{}",a);

    重影与可变变量的赋值不是一个概念,重影是指用同一个名字重新代表另一个变量实体,其类型、可变属性和值都可以变化。但可变变量赋值仅能发生值的变化。

  • 相关阅读:
    猿创征文|时间序列分析算法之平稳时间序列预测算法和自回归模型(AR)详解+Python代码实现
    门店数字化转型| 美容院管理系统
    Small Pipefish
    赞奇科技参与华为云828 B2B企业节,云工作站入选精选产品解决方案
    SpringBoot + openFeign实现远程接口调用
    HTML5期末考核大作业、HTML个人主页界面设计源码
    Python之禅——跟老吕学Python编程
    【亲测有效】1分钟 一种更简单的方法安装高匿名http协议(没有账号密码功能)代理服务 步骤超简单 仅限用于学习交流使用 勿用于其他用途
    Flink Postgres CDC
    关于Redis的远程连接 Connection: Disconnect on error 问题
  • 原文地址:https://blog.csdn.net/keny88888/article/details/133319599