首页 > 其他分享 >Rust语言体验

Rust语言体验

时间:2023-01-18 04:22:06浏览次数:53  
标签:cargo 语言 -- sample Rust https 体验 rust

保命声明:笔者代码能力有限,若行文中有错漏之处欢迎大家指出。

Rust语言

一门赋予每个人构建可靠且高效软件能力的语言。Rust 速度惊人且内存利用率极高。由于没有运行时和垃圾回收,它能够胜任对性能要求特别高的服务,可以在嵌入式设备上运行,还能轻松和其他语言集成。Rust 丰富的类型系统和所有权模型保证了内存安全和线程安全,让您在编译期就能够消除各种各样的错误。Rust 拥有出色的文档、友好的编译器和清晰的错误提示信息, 还集成了一流的工具——包管理器和构建工具, 智能地自动补全和类型检验的多编辑器支持, 以及自动格式化代码等等针对资源匮乏的设备?需要底层控制而又不失上层抽象的便利?Rust 包您满意!

配置环境

[https://www.rust-lang.org/zh-CN/learn/get-started]
[https://zhuanlan.zhihu.com/p/357909664]

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y --no-modify-path
vim ~/.bash_profile
#添加source "$HOME/.cargo/env"
source ~/.bash_profile
rustup update
rustc --version

快速体验

在线编辑器

[https://play.rust-lang.org]

fn main() {
    println!("Hello, rust \n --qsbye!");
}

本机环境

  1. VS Code安装rust-analyzer(The Rust Programming Language)插件.
  2. 新建rust_learn文件夹
  3. 创建项目:cargo new rust-bonjour & cd rust-bonjour

Cargo.toml ,这个类似于 Node.js 中的 package.json 声明了项目所需的信息,对于 Rust 项目来说就是声明了 Cargo 编译程序包所需的元数据,以 .toml 文件格式编写。

  1. 修改src/main.rs
fn main() {
    println!("Bonjour, rust! \n --qsbye");
}
  1. 编译cargo build或者直接cargo run
  2. 运行target/debug/rust-boujour

斐波那契数列

fn main() {
    //     斐波那契数列
    let mut index = 1;
    while index <= 10 {
        println!("{} 阶斐波那契数列的值:{}", index, fb(index));
        index = index + 1;
    }
}

fn fb(n: i32) -> i32 {
    let mut first = 1;
    let mut secent = 1;
    if n <= 2 {
        1
    } else {
        let mut index = 3;
        let mut result = 1;
        while index <= n {
            index = index + 1;
            result = first + secent;
            first = secent;
            secent = result;
        }
        result
    }
}

运行:cargo run

Rust嵌入式

[https://www.rust-lang.org/zh-CN/what/embedded]
[https://jzow.github.io/discovery/]
在编译时强制要求引脚和外设配置,以确保资源不会被您应用程序中非预期的部分使用。动态内存分配是可选的。可使用全局分配器和动态数据结构,也可以不采用堆而全部静态分配。Rust 使得线程之间不可能意外地共享状态。无论使用哪种喜欢的方式实现并发,都能获得 Rust 提供的强力保障。可将 Rust 集成到现有的 C 代码库中,也可利用现有的 SDK 编写 Rust 应用程序。只需要一次编写库或驱动,就能在各种系统中使用它。不管是非常小的微控制器,还是强大的单板计算机,都可直接使用。作为 Rust 开源项目的一部分,嵌入式系统由一流的并拥有商业伙伴支持的开源社区驱动。

用Rust开发Arduino Uno(AVR架构)

[http://jakegoulding.com/blog/2016/01/02/rust-on-an-arduino-uno/]
[https://github.com/shepmaster/rust-arduino-blink-led-no-core/tree/part2]
[https://github.com/shepmaster/rust-arduino-blink-led-no-core-with-cargo]
预期现象:Arduino Uno上的LED灯闪烁
This code shows how to blink an LED on the Arduino Uno in the hardest possible way.

准备交叉编译器

提前在网页中登录github,以防LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443

cargo new rust_arduino && cd rust_arduino
git clone https://github.com/avr-rust/rust-legacy-fork.git
cd rust-legacy-fork 
mkdir build && cd build
../configure
make
#然后会下载一堆模块,等待时间可以喝几杯奶茶了

标签:cargo,语言,--,sample,Rust,https,体验,rust
From: https://www.cnblogs.com/qsbye/p/17058721.html

相关文章