rust学习1-入门

Rust学习1-入门

安装Rust &VScode集成Rust

目标:

  • windows 环境下使用VScode IDE 进行rust编写和测试运行

  • VScode集成rust

windows 安装Rust

It looks like you’re running Windows. To start using Rust, download the installer, then run the program and follow the onscreen instructions. You may need to install the Visual Studio C++ Build tools when prompted to do so. If you are not on Windows see “Other Installation Methods”.

  1. 安装 Visual Studio 2019 的构建工具

    https://visualstudio.microsoft.com/visual-cpp-build-tools/

    语言包: 选择中文和英文

    image-20220203173414217

  2. 安装rust

    https://www.rust-lang.org/tools/install 选择64位

    默认安装即可

  3. 测试是否安装成功:

    $ cargo --version
    cargo 1.58.0 (f01b232bc 2022-01-19)
    

VSCode集成rust

  1. 安装Rust and Friends 集成包

    image-20220203173823339

  2. 集成包中包括: rust-analyzer

    使用rust-analyzer 进行编译运行

Rust语言

基元类型

bool,char

整型 最大支持128位

有符号 无符号
i8 u8
i16 u16
i32 u32
i64 u64
i126 u128
isize 尺寸可变的有符号整型
usize 尺寸可变的无符号整型
f32 32位浮点数
f64 64位浮点数
[T,N] 固定大小的数组, T元素类型,N数组大小
[T] 动态大小的连续序列的视图,T任意类型
str 字符串切片,主要做引用, 即&str
(T,U,…) 有限序列,T,U为不同类型
fn(i32) -> i32 接受i32类型参数并返回i32类型参数的函数

变量声明和不可变性

使用关键字let声明变量, 变量初始,无法分配其他值,若要修改,则前面添加mut关键字

fn main() {
   
    let a = 1;
    let mut b = 2;
    a = 3;
    b = 4;
    print!("{},{}",a,b);
}

报错.

image-20220203152052669

函数

函数表示:

fn (u64,u64) ->u64

闭包

简单闭包, 通过a来调用, ||内存放参数

let a = ||();

实例:

fn main() {
   
    let double = |x| x*2;
    let value = 5;
    let twice = double(value);
    let big_c = |b,c| {
   
        let z = b+c;
        z * twice
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值