RUST快速入门

历史记录

version修改记录修改人修改时间
0.1初始版本linux-logic2025年2月2日

安装rust编译环境

For linux

  • 下载相关准备资源
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • 选择1,标准化安装
linux-logic@logic:~/tools/rust$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

...

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
  • 重新启动当前shell以加载环境变量(安装程序会自动修改.bashrc环境变量配置文件,在文件最后增加. "$HOME/.cargo/env"),验证是否安装正确:
linux-logic@logic:~$ rustc --version
rustc 1.84.1 (e71f9a9a9 2025-01-27)
  • rust工具支持更新命令:rustup update

VScode适配

适配方法

安装插件:

  • 输入Ctrl+Shift+X打开插件管理器
  • 搜索rust-analyzer并安装

安装成功后会显示:

主要功能

功能快捷键作用
refactoringF2格式化代码格式
自动跳转Ctrl+鼠标左键调转到函数定义
Quick FixesCtrl+.快速补全
Go to DefinitionF12跳转到类型定义
Show Call HierarchyShift+Alt+H显示该函数所有调用关系
Command PaletteCtrl+Shift+P查找某个符号

helloword

cargo new hello_world

自动生成以下文件(同时也会自动生成git文件,包括.gitignore):

linux-logic@logic:~/workspace/rust_workspace$ tree
.
`-- hello_world
    |-- Cargo.toml
    `-- src
        `-- main.rs

2 directories, 2 files

编译运行命令:

cargo build
cargo run
/* 或者直接运行可执行程序 */
hello_world/target/debug/hello_world

参考链接

rust官方网站:https://www.rust-lang.org/
《The Rust Programming Language》在线阅读:https://doc.rust-lang.org/stable/book/#the-rust-programming-language
安装vscode插件:https://code.visualstudio.com/docs/languages/rust