暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

程序员秘书 2022-08-30
1693
哈喽,大家好,我是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
目录中,可以在这里找到包括 rustc
cargo
rustup
在内的 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 中格式字符串中的占位符不是 "% + 字母"
的形式,而是一对 {}

点击↑↑↑关注、后台撩我
交流经验,交流技术,交个朋友


最近部分文章汇总

Linux&Android相关:

Android java、native、kernel获取堆栈信息常用方法总结

Android开发带有odex, oat, vdex, art等后缀的文件,都是什么含义?

Android 13要默认使用华为 EROFS 超级文件系统了

Linux split 命令在这种情况下很有用

借助Linux shell脚本搞定一个开机异常问题

Linux ftrace 之 function、function_graph 使用笔记(一)

Linux环境下如何正确安装scons

Linux shell 语法  if [ $? == 0 ]  详细

创建Linux用户并获得root权限且在window下实现访问

常见.zip文件该如何操作才能正确解压

早知道这两种命令工具,早高效~

linux下查找文件,看这篇就够了

新入职,尽快平移老员工的环境配置是正道

Linux远程拷贝文件scp命令详细

嵌入式零碎:

5分钟!搞懂NTC热敏电阻!

谈谈手机的指纹识别技术,贴膜是对我最大的...

手机屏幕封装技术及其分类相关知识

CPU、ARM、架构(及指令集)关系

我应该更早一点认识Git

大端小端详解(含代码及详细注释)

源码中看到一个.hpp文件,差点闹了笑话

 Hi~main~

C/C++细碎

磨刀不误砍柴工

程序人生:

程序员不应该就是专职敲代码的吗

有关“前途和钱途”的一点思考

媳妇电脑卡顿了,我有事干了!

>>>如果觉得文章有用,记得点赞、在看 鼓励~ 


文章转载自程序员秘书,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论