加入星计划,您可以享受以下权益:

  • 创作内容快速变现
  • 行业影响力扩散
  • 作品版权保护
  • 300W+ 专业用户
  • 1.5W+ 优质创作者
  • 5000+ 长期合作伙伴
立即加入
  • 正文
  • 推荐器件
  • 相关推荐
  • 电子产业图谱
申请入驻 产业图谱

初试Rust,以后 linux 驱动代码可能要用 Rust 来写了

2023/07/30
4733
阅读需 21 分钟
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

哈喽,大家好,我是LittleG。

前言

之前文章《Android java、native、kernel获取堆栈信息常用方法总结》有提到编程语言是工具。我最近刚刚入手了一个新的工具,就是 Rust ,记录分享下。

简单介绍下 Rust :

Rust 是最近几年很火的一门编程语言,由 Mozilla 公司开发。据说安全性要比 C 或 C++ 语言好,而且效率可以媲美 C 或 C++ 语言。目前谷歌在力推,正在逐步替换进Android。貌似 Linux 内核也在接受改变,网上有传,占据linux内核代码将近一半的驱动代码,有率先被 Rust 替换开发的可能;也就是说,以后我们写 linux 驱动代码,很有可能就要用 Rust 来写了

了解几个 Rust 相关概念:

rustup 是 rust 官方推出的基于 终端/控制台/shell 的工具链管理工具,可用于管理 rust 版本和相关工具,如安装哪个版本的rust和常用组件等。

cargo 是用 rust 写的一个包管理工具(可以直接在http://crates.io上搜到并当作依赖来用) + 工程管理工具,类似c++ 等同于conan+cmake+make;可以用来构建工程,最后编译还是用 rustc。

rustc 是 rust 的编译器。类似 c++ 等同于gcc g++。

我决定尝鲜体验 rust 的环境是 ubuntu 22.04,考虑到是刚新装ok的ubuntu 22.04,所以在下载安装rust之前,我先需要更新下ubuntu 包管理工具的镜像源,更新为国内的镜像源,避免被墙的问题,下载速度也会快很多。网上找了 ubuntu 22.04 的 tsinghua 镜像源,配置如下:

1、修改保存到 /etc/apt/sources.list

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

2、保存/etc/apt/sources.list并退出后,更新deb源即可;

sudo apt update

等待更新完成即可;

镜像源更新ok,然后就可以开始安装 rust 了。

Linux下安装命令为curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

james@linux:~$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

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:

  /home/james/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/james/.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:

  /home/james/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/james/.profile
  /home/james/.bashrc
  ......
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     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-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2022-07-19, rust version 1.62.1 (e092d0b6b 2022-07-16)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'rustfmt'
......
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.62.1 (e092d0b6b 2022-07-16)

Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source "$HOME/.cargo/env"

看到如上输出,说明 rust 基本组件安装完成了。注意按照最后输出的提示重新当前shell终端,然后执行:

james@linux:~$ source "$HOME/.cargo/env"

在 Rust 开发环境中,所有工具都安装在 ~/.cargo/bin 目录中,可以在这里找到包括 rustccargorustup 在内的 Rust 工具链。如:

james@linux:~/.cargo/bin$ ls -l
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 cargo
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 cargo-clippy
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 cargo-fmt
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 cargo-miri
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 clippy-driver
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 rls
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 rust-gdb
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 rust-gdbgui
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 rust-lldb
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 rustc
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 rustdoc
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 rustfmt
-rwxr-xr-x 13 james james 15825920 Aug  1 17:20 rustup

如果环境OK,通过执行rustc --version 就可以看到具体的版本号了。

接下来,就可以使用 rust 写一个简单的 helloworld 程序,验证一下 rust 编译环境是否正常了。

如我写了一个简单的 Hello.rs

fn main() {
    println!("Hello world!");
    println!("Nice to meeting you!");
}

编译 rustc Hello.rs

james@linux:~$ rustc Hello.rs

提一下:

编译时可能会碰到,提示linker cc not found链接错误:

error: linker `cc` not found
|
= note: No such file or directory (os error 2)

error: aborting due to previous error

原因可能是因为本地的 gcc 没有安装 或者  没有对应的 cc 软连接 ,需要自己手动安装一下或者手动建立一下软链接:

sudo apt install gcc
sudo ln -s gcc cc

注意,如果安装后还提示 gcc 版本或者依赖之类的错误,则可以使用ubuntu特有的可以解决依赖关系的包管理工具 aptitude进行安装,解决依赖,如果没有 aptitude,可以先安装之,

apt install aptitude
aptitude install gcc

通过 aptitude安装,会给出解决依赖的方案,注意查看输出提示,选择操作即可。

如果没有编译错误,执行  rustc Hello.rs后,就会看到当前目录下会多出来一个同名的可执行文件:

james@linux:~/$ ls
Hello  Hello.rs
james@linux:~/$ file Hello
Hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=xxxxxxxxxxxxxxxxxxxxx, for GNU/Linux 3.2.0, with debug_info, not stripped

既然是可执行文件,说明我们和 C语言编译出来的  Linux C 程序一样执行应该就可以,试一下:

james@linux:~/$ ./Hello
Hello world!
Nice to meeting you!

成功输出,说明 rust 环境和工具链的安装就绪了。

以后如果想卸载 Rust,可以执行:rustup self uninstall

以后如果想更新 Rust,可以执行:rustup update

最后再简单说明下测试程序:

// rust test
fn main() {
    println!("Hello world!");
    println!("Nice to meeting you!");
}

1、Rust 使用 fn 关键字定义一个函数;

2、Rust 的打印日志语句,println 后面都跟一个 感叹号 ( ! ),指明println!() 是 Rust 中的一个 预定义的宏,打印输出是一个 宏调用。在 rust 中区分函数和宏的唯一办法,就是看函数名/宏名最后有没有 感叹号 !. 如果有感叹号则是宏,没有则是函数。

3、Rust 代码注释,和 C语言、C++是一样的语法;

4、Rust 输出文字的方式主要有两种:println!()print!()。这两个"函数"都是向命令行输出字符串的方法,区别仅在于前者会在输出的最后附加输出一个换行符。当用这两个"函数"输出信息的时候,第一个参数是格式字符串,后面是一串可变参数,对应着格式字符串中的"占位符",这一点与 C 语言中的 printf 函数很相似。但是,Rust 中格式字符串中的占位符不是 "% + 字母" 的形式,而是一对 {}

 

推荐器件

更多器件
器件型号 数量 器件厂商 器件描述 数据手册 ECAD模型 风险等级 参考价格 更多信息
HFBR-2416Z 1 Foxconn Receiver, 160Mbps, ST Connector, Through Hole Mount, ROHS COMPLIANT, PLASTIC, 8 PIN
$24.86 查看
TLP187(TPL,E 1 Toshiba America Electronic Components Darlington Output Optocoupler, 1-Element, 3750V Isolation

ECAD模型

下载ECAD模型
$1.37 查看
HCNW3120-300E 1 Broadcom Limited Logic IC Output Optocoupler, 1-Element, 5000V Isolation, 0.400 INCH, ROHS COMPLIANT, SURFACE MOUNT, DIP-8
$5.82 查看

相关推荐

电子产业图谱

记录和分享C/C++、Linux、ARM、Android、IoT相关知识。技术相伴于生活和成长,愿你我永为少年,心中有火,眼中有光,始保热情。