飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

Rust极简教程-

时间:2022-04-01  作者:Naylor  

目录
  • 简介
    • 特性
    • 特征
    • 用途
  • 安装
  • 核心组件
  • 常用命令
  • 基础语法
    • 数据类型
      • 标量类型
      • 复合类型
      • 示例
    • 条件语句
    • 循环
    • 输出&输入
      • 输出
        • 输出花括号
        • 输出非基础类型
      • 输入
  • 所有权
  • 切片
  • 结构体
  • 枚举
    • Match
    • If-let
    • Option
  • 集合
    • vector容器
    • String
  • 代码组织
    • 命名空间
    • 访问其他mod和crate
      • 访问权限和关键字
      • 访问其他文件中的对象
    • 使用第三方库
  • 异常处理
  • 泛型
    • 泛型概念
  • 特性(接口)
  • 文件和IO
  • 面向对象
  • 并发编程
    • 线程
    • 消息传递
    • 互斥锁
  • 代码
  • 说明
  • 引用

简介

Rust是一门赋予每个人构建可靠且高效软件能力的编程语言。可靠主要体现在安全性上。其高效不仅限于开发效率,它的执行效率也是令人称赞的,是一种少有的兼顾开发效率和执行效率的语言。Rust 语言由 Mozilla 开发,最早发布于 2014 年 9 月。Rust 的编译器是在 MIT License 和 Apache License 2.0 双重协议声明下的免费开源软件。

特性

  • 高性能:Rust 速度惊人且内存利用率极高。由于没有运行时和垃圾回收,它能够胜任对性能要求特别高的服务,可以在嵌入式设备上运行,还能轻松和其他语言集成。

  • 可靠性:Rust 丰富的类型系统和所有权模型保证了内存安全和线程安全,让您在编译期就能够消除各种各样的错误。

  • 生产力:Rust 拥有出色的文档、友好的编译器和清晰的错误提示信息, 还集成了一流的工具——包管理器和构建工具, 智能地自动补全和类型检验的多编辑器支持, 以及自动格式化代码等等。

  • Rustacean:使用 rust 的攻城狮不叫 ruster 而是叫 Rustacean ,咱也不知道为什么,书上就是这么说的。

特征

  • 作为一门编程语言,rust既可以分类为面向过程编程语言,也可以分类为面向对象编程语言
  • rust拥有精细化的基础数据结构
  • rust中支持泛型,并且拥有泛型枚举
  • rust中支持接口,甚至支持接口默认方法,并且接口既可以作为方法入参也可以作为方法出参

用途

  • 传统命令行程序
  • 嵌入式
  • 网络服务
  • WebAssembly
  • Web服务
  • ......

安装

以 windows 11 为例

下载 rustup-域名 ,双击此可执行程序会打开一个命令行程序,此程序引导安装,具体安装过程:

Rust Visual C++ prerequisites

Rust requires the Microsoft C++ build tools for Visual Studio 2013 or
later, but they don\'t seem to be installed.

The easiest way to acquire the build tools is by installing Microsoft
Visual C++ Build Tools 2019 which provides just the Visual C++ build
tools:

  https://域名/visual-cpp-build-tools/

Please ensure the Windows 10 SDK and the English language pack components
are included when installing the Visual C++ Build Tools.

Alternately, you can install Visual Studio 2019, Visual Studio 2017,
Visual Studio 2015, or Visual Studio 2013 and during install select
the "C++ tools":

  https://域名/downloads/

Install the C++ build tools before proceeding.

If you will be targeting the GNU ABI or otherwise know what you are
doing then it is fine to continue installation without the build
tools, but otherwise, install the C++ build tools before proceeding.

Continue? (y/N) y


Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  C:\Users\cml\.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  C:\Users\cml\.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo\'s bin directory, located at:

  C:\Users\cml\.cargo\bin

This path will then be added to your PATH environment variable by
modifying the HKEY_CURRENT_USER/Environment/PATH registry key.

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-pc-windows-msvc
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>

info: profile set to \'default\'
info: default host triple is x86_64-pc-windows-msvc
info: syncing channel updates for \'stable-x86_64-pc-windows-msvc\'
info: latest update on 2022-01-20, rust version 域名.1 (db9d1b20b 2022-01-20)
info: downloading component \'cargo\'
  3.8 MiB /   3.8 MiB (100 %)   1.7 MiB/s in  2s ETA:  0s
info: downloading component \'clippy\'
  1.6 MiB /   1.6 MiB (100 %)   1.5 MiB/s in  1s ETA:  0s
info: downloading component \'rust-docs\'
 18.8 MiB /  18.8 MiB (100 %)   3.3 MiB/s in  5s ETA:  0s
info: downloading component \'rust-std\'
 22.9 MiB /  22.9 MiB (100 %)   3.2 MiB/s in  7s ETA:  0s
info: downloading component \'rustc\'
 65.2 MiB /  65.2 MiB (100 %) 493.2 KiB/s in  1m 14s ETA:  0s
info: downloading component \'rustfmt\'
  2.2 MiB /   2.2 MiB (100 %) 631.2 KiB/s in  3s ETA:  0s
info: installing component \'cargo\'
info: installing component \'clippy\'
info: installing component \'rust-docs\'
 18.8 MiB /  18.8 MiB (100 %)   1.9 MiB/s in  6s ETA:  0s
info: installing component \'rust-std\'
 22.9 MiB /  22.9 MiB (100 %)  10.5 MiB/s in  2s ETA:  0s
info: installing component \'rustc\'
 65.2 MiB /  65.2 MiB (100 %)  12.2 MiB/s in  5s ETA:  0s
info: installing component \'rustfmt\'
info: default toolchain set to \'stable-x86_64-pc-windows-msvc\'

  stable-x86_64-pc-windows-msvc installed - rustc 域名.1 (db9d1b20b 2022-01-20)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload its PATH environment variable to include
Cargo\'s bin directory (%USERPROFILE%\.cargo\bin).

Press the Enter key to continue.

核心组件

  • rustup:安装、更新rust用的,rustup doc 可查看安装包中的官方指导文档
  • cargo:包管理器和编译源代码工具
  • rustc:将rust源文件编译成可执行程序
  • rustdoc:为rust项目生成说明文档

常用命令

命令 说明 备注
rustup doc 打开官方指导文档
cargo new projectName 创建一个rust工程 示例:cargo new firstRustProject
cargo run 运行rust工程
cargo build 编译rust工程 若增加了依赖,即修改了toml文件,需要重新编译

基础语法

  • fn 声明函数
  • let 声明变量,变量默认不可变,加 mut 的变量为可变变量
  • 虽然所有变量声明都用let关键字,但是rust是强类型的语言,内置了强大的类型推导
  • 函数参数:参数名:参数数据类型
  • 变量重影性质:同一个名称可以被多个变量使用,后面的变量会覆盖前面的变量
  • const 声明常量
  • //注释一行,/**/注释多行
  • 使用英文分号换行

示例:


fn main() {
    println!("Hello, world!");
       //变量默认是不可变的,加上 mut 关键字就可以重新赋值。
       let mut  x=5;
       println!("The value of x is   {}  ",x);
   
       x=6;
       println!("The value of x is   {}  ",x);
   
   
       //变量的隐藏
       let  money=100;
       println!("money is {}",money);
       let money =money+8;
       println!("money is {}",money);
       let money="一百元";
       println!("money is {}",money);
   
       //常量使用 const 关键字声明,声明的时候必须指定数据类型,常量名全大写。
       //不需要let , 不可使用mut 修饰
       const MAX_PIONTS: u32=888;
       println!("The constant is {}",MAX_PIONTS);
       
    let  result:char= a_function(88, \'M\', false);
    println!("result is {}",result);
}
fn a_function(a:u64,b:char,c:bool)-> char{
    println!("a is {}",a);
    println!("b is {}",b);
    println!("c is {}",c);
    return  \'N\';
}


输出:

Hello, world!
The value of x is   5
The value of x is   6
money is 100
money is 108
money is 一百元
The constant is 888
a is 88
b is M
c is false
result is N

数据类型

标量类型

整数,浮点,布尔,字符

  • 整数:i8,u8,i16,u16,i32,u32,i64,u64,i128,u128,isize,usize
    • i表示有符号
    • u表示无符号
    • isize 和 usize 两种整数类型是用来衡量数据大小的,它们的位长度取决于所运行的目标平台,如果是 32 位架构的处理器将使用 32 位位长度整型。
  • 浮点:f32,f64

复合类型

可以将多个值放到一个数据类型中。

  • 元组-tuple。长度固定,元素的数据类型可以不同
  • 数组,长度固定,元素的数据类型必须相同
  • Vector:不是标准库提供的。和数组类似,长度可变

示例


fn main() {
    println!("Hello, world!");
    let q=3.0;
    let q:f32=域名;
    let w=true;
    let r:bool =false;
 
    let t=\'

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。