加入星計劃,您可以享受以下權(quán)益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴(kuò)散
  • 作品版權(quán)保護(hù)
  • 300W+ 專業(yè)用戶
  • 1.5W+ 優(yōu)質(zhì)創(chuàng)作者
  • 5000+ 長期合作伙伴
立即加入
  • 正文
  • 推薦器件
  • 相關(guān)推薦
  • 電子產(chǎn)業(yè)圖譜
申請入駐 產(chǎn)業(yè)圖譜

初試Rust,以后 linux 驅(qū)動代碼可能要用 Rust 來寫了

2023/07/30
3521
閱讀需 21 分鐘
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點資訊討論

哈嘍,大家好,我是LittleG。

前言

之前文章《Android java、native、kernel獲取堆棧信息常用方法總結(jié)》有提到編程語言是工具。我最近剛剛?cè)胧至艘粋€新的工具,就是 Rust ,記錄分享下。

簡單介紹下 Rust :

Rust 是最近幾年很火的一門編程語言,由 Mozilla 公司開發(fā)。據(jù)說安全性要比 C 或 C++ 語言好,而且效率可以媲美 C 或 C++ 語言。目前谷歌在力推,正在逐步替換進(jìn)Android。貌似 Linux 內(nèi)核也在接受改變,網(wǎng)上有傳,占據(jù)linux內(nèi)核代碼將近一半的驅(qū)動代碼,有率先被 Rust 替換開發(fā)的可能;也就是說,以后我們寫 linux 驅(qū)動代碼,很有可能就要用 Rust 來寫了。

了解幾個 Rust 相關(guān)概念:

rustup 是 rust 官方推出的基于 終端/控制臺/shell 的工具鏈管理工具,可用于管理 rust 版本和相關(guān)工具,如安裝哪個版本的rust和常用組件等。

cargo 是用 rust 寫的一個包管理工具(可以直接在http://crates.io上搜到并當(dāng)作依賴來用) + 工程管理工具,類似c++ 等同于conan+cmake+make;可以用來構(gòu)建工程,最后編譯還是用 rustc。

rustc 是 rust 的編譯器。類似 c++ 等同于gcc g++。

我決定嘗鮮體驗 rust 的環(huán)境是 ubuntu 22.04,考慮到是剛新裝ok的ubuntu 22.04,所以在下載安裝rust之前,我先需要更新下ubuntu 包管理工具的鏡像源,更新為國內(nèi)的鏡像源,避免被墻的問題,下載速度也會快很多。網(wǎng)上找了 ubuntu 22.04 的 tsinghua 鏡像源,配置如下:

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

# 默認(rèn)注釋了源碼鏡像以提高 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

# 預(yù)發(fā)布軟件源,不建議啟用
# 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 基本組件安裝完成了。注意按照最后輸出的提示重新當(dāng)前shell終端,然后執(zhí)行:

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

在 Rust 開發(fā)環(huán)境中,所有工具都安裝在 ~/.cargo/bin 目錄中,可以在這里找到包括 rustc、cargorustup 在內(nèi)的 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

如果環(huán)境OK,通過執(zhí)行rustc --version 就可以看到具體的版本號了。

接下來,就可以使用 rust 寫一個簡單的 helloworld 程序,驗證一下 rust 編譯環(huán)境是否正常了。

如我寫了一個簡單的 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 沒有安裝 或者 ?沒有對應(yīng)的 cc 軟連接 ,需要自己手動安裝一下或者手動建立一下軟鏈接:

sudo apt install gcc
sudo ln -s gcc cc

注意,如果安裝后還提示 gcc 版本或者依賴之類的錯誤,則可以使用ubuntu特有的可以解決依賴關(guān)系的包管理工具 aptitude進(jìn)行安裝,解決依賴,如果沒有 aptitude,可以先安裝之,

apt install aptitude
aptitude install gcc

通過 aptitude安裝,會給出解決依賴的方案,注意查看輸出提示,選擇操作即可。

如果沒有編譯錯誤,執(zhí)行 ?rustc Hello.rs后,就會看到當(dāng)前目錄下會多出來一個同名的可執(zhí)行文件:

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

既然是可執(zhí)行文件,說明我們和 C語言編譯出來的 ?Linux C 程序一樣執(zhí)行應(yīng)該就可以,試一下:

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

成功輸出,說明 rust 環(huán)境和工具鏈的安裝就緒了。

以后如果想卸載 Rust,可以執(zhí)行:rustup self uninstall

以后如果想更新 Rust,可以執(zhí)行:rustup update

最后再簡單說明下測試程序:

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

1、Rust 使用 fn 關(guān)鍵字定義一個函數(shù);

2、Rust 的打印日志語句,println 后面都跟一個 感嘆號 ( ! ),指明println!() 是 Rust 中的一個 預(yù)定義的宏,打印輸出是一個 宏調(diào)用。在 rust 中區(qū)分函數(shù)和宏的唯一辦法,就是看函數(shù)名/宏名最后有沒有 感嘆號 !. 如果有感嘆號則是宏,沒有則是函數(shù)。

3、Rust 代碼注釋,和 C語言、C++是一樣的語法;

4、Rust 輸出文字的方式主要有兩種:println!()print!()。這兩個"函數(shù)"都是向命令行輸出字符串的方法,區(qū)別僅在于前者會在輸出的最后附加輸出一個換行符。當(dāng)用這兩個"函數(shù)"輸出信息的時候,第一個參數(shù)是格式字符串,后面是一串可變參數(shù),對應(yīng)著格式字符串中的"占位符",這一點與 C 語言中的 printf 函數(shù)很相似。但是,Rust 中格式字符串中的占位符不是 "% + 字母" 的形式,而是一對 {}。

 

推薦器件

更多器件
器件型號 數(shù)量 器件廠商 器件描述 數(shù)據(jù)手冊 ECAD模型 風(fēng)險等級 參考價格 更多信息
JST01TMAC1CY5GE2 1 Viavi Solutions Inc Transceiver,
暫無數(shù)據(jù) 查看
6N137-X007T 1 Vishay Intertechnologies Optocoupler Logic-Out Open Collector DC-IN 1-CH 8-Pin PDIP SMD T/R

ECAD模型

下載ECAD模型
$2.22 查看
TJA1055T/3/CM,118 1 NXP Semiconductors TJA1055 - Enhanced fault-tolerant CAN transceiver SOIC 14-Pin

ECAD模型

下載ECAD模型
$1.95 查看

相關(guān)推薦

電子產(chǎn)業(yè)圖譜

記錄和分享C/C++、Linux、ARM、Android、IoT相關(guān)知識。技術(shù)相伴于生活和成長,愿你我永為少年,心中有火,眼中有光,始保熱情。