rust abc(1): 最小环境搭建

news2024/10/6 4:01:39

文章目录

    • 1. 目的
    • 2. 命令集合
    • 3. 安装或更新 rust
      • 3.1 命令
      • 3.2 运行结果
    • 4. 包管理工具 Cargo
    • 5. 创建 Rust 的 Hello World 程序: 单个文件
    • 6. 创建 Rust 的 Hello World 工程: 基于 Cargo
      • 6.1 cargo new 创建工程
      • 6.2 cargo run
      • 6.3 完整输出
      • 6.4 解释
    • 7. IDE/编辑器
    • 8. References

1. 目的

安装 rust 语言需要的开发环境, 包括必要的工具链, 以及可选的 IDE/编辑器.

2. 命令集合

  • rustc: rust 编译器, 用来编译 .rs 后缀的文件
  • rustup: rust 工具链安装程序(命令行), 用来安装、更新 rustc、cargo 以及编译工具链,比如交叉编译的工具链
  • cargo: rust 的工程管理工具, 它自称为“rust 包管理器”,但其实还肩负着每个工程的构建、清理等任务

3. 安装或更新 rust

3.1 命令

第一次安装:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

安装了好久,rust 都 rust 了(rust 本意是铁锈),还没有开启学习 rust 的你,请务必更新 rust 到最新:

rustup update

查看 rust 版本:

rustc -V

3.2 运行结果

我是在 Ubuntu 22.04 下运行的。今天是 2023年06月24日。 rust 更新后是 1.70.0 版本:

zz@Legion-R7000P% rustup update
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
732.1 KiB / 732.1 KiB (100 %) 175.4 KiB/s in  2s ETA:  0s
info: latest update on 2023-06-01, rust version 1.70.0 (90c541806 2023-05-31)
info: downloading component 'rust-src'
  2.3 MiB /   2.3 MiB (100 %)   1.7 MiB/s in  1s ETA:  0s
info: downloading component 'cargo'
  6.9 MiB /   6.9 MiB (100 %)   2.4 MiB/s in  3s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 13.5 MiB /  13.5 MiB (100 %)   3.7 MiB/s in  4s ETA:  0s
info: downloading component 'rust-std'
 27.3 MiB /  27.3 MiB (100 %)   4.6 MiB/s in  6s ETA:  0s
info: downloading component 'rustc'
 64.3 MiB /  64.3 MiB (100 %)   4.2 MiB/s in 15s ETA:  0s
info: downloading component 'rustfmt'
info: removing previous version of component 'rust-src'
info: removing previous version of component 'cargo'
info: removing previous version of component 'clippy'
info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: removing previous version of component 'rustfmt'
info: installing component 'rust-src'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
 27.3 MiB /  27.3 MiB (100 %)  19.6 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 64.3 MiB /  64.3 MiB (100 %)  21.1 MiB/s in  3s ETA:  0s
info: installing component 'rustfmt'
info: checking for self-update

  stable-x86_64-unknown-linux-gnu updated - rustc 1.70.0 (90c541806 2023-05-31) (from rustc 1.69.0 (84c898d65 2023-04-16))

info: cleaning up downloads & tmp directories
zz@Legion-R7000P% rustc -V
rustc 1.70.0 (90c541806 2023-05-31)

4. 包管理工具 Cargo

cargo 是一个命令行工具, 安装 rust 后自带的。当前 (2023-06-24 19:27:29) cargo 版本也是 1.70.0。

zz@Legion-R7000P% cargo -V
cargo 1.70.0 (ec8a8a0ca 2023-04-25)
zz@Legion-R7000P% rustc -V
rustc 1.70.0 (90c541806 2023-05-31)

5. 创建 Rust 的 Hello World 程序: 单个文件

最简单的额 rust 程序, 只有一个 .rs 文件, 不需要让 cargo 来下掺和。

hello.rs:

fn main()
{
    println!("Hello, World!");
}

编译和运行:

rustc hello.rs
./hello

输出:

zz@Legion-R7000P% rustc hello.rs
zz@Legion-R7000P% ./hello
Hello, World!
zz@Legion-R7000P% ls
hello  hello.rs

6. 创建 Rust 的 Hello World 工程: 基于 Cargo

6.1 cargo new 创建工程

既然 Rust 官方让我们用 Cargo, 那就试用一下:

zz@Legion-R7000P% ls
hello  hello.rs
zz@Legion-R7000P% pwd
/home/zz/work/rust_abc
zz@Legion-R7000P% cargo new hello-rust
     Created binary (application) `hello-rust` package
zz@Legion-R7000P% tree -L 3
.
├── hello
├── hello.rs
└── hello-rust
    ├── Cargo.toml
    └── src
        └── main.rs

2 directories, 4 files

在这里插入图片描述

其中:

  • Cargo.toml 是 manifest 文件,保存了工程的 metadata 和依赖项
[package]
name = "hello-rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
  • src/main.rs 是主代码
fn main() {
    println!("Hello, world!");
}

6.2 cargo run

我们像小白鼠一样,按官方文档直接操作。刚才已经执行过了

cargo new hello-rust

现在执行

cargo run

发现报错了:

error: could not find Cargo.toml in /home/zz/work/rust_abc or any parent directory

嗯,官方教程1 也写的挺不仔细的, 少一个切换路径的操作:

cd hello-rust  # 进入到我们刚才创建的工程目录中

然后才是运行:

cargo run

就看到了想要的输出:

zz@Legion-R7000P% cargo run
Compiling hello-rust v0.1.0 (/home/zz/work/rust_abc/hello-rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.19s
Running target/debug/hello-rust
Hello, world!

6.3 完整输出

zz@Legion-R7000P% cargo run
error: could not find `Cargo.toml` in `/home/zz/work/rust_abc` or any parent directory
zz@Legion-R7000P% cargo run hello-rust
error: could not find `Cargo.toml` in `/home/zz/work/rust_abc` or any parent directory
zz@Legion-R7000P% cd hello-rust
zz@Legion-R7000P% cargo run
   Compiling hello-rust v0.1.0 (/home/zz/work/rust_abc/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.19s
     Running `target/debug/hello-rust`
Hello, world!

6.4 解释

cargo run 干了很多事情:

  • 编译, 得到了 target/debug/hello-rust 文件
  • 运行,运行的是 target/debug/hello-rust, 输出了 “Hello, world!”
  • 还有一些其他杂七杂八的文件
zz@Legion-R7000P% pwd
/home/zz/work/rust_abc/hello-rust
zz@Legion-R7000P% tree -L 4
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    └── debug
        ├── build
        ├── deps
        │   ├── hello_rust-741c35951fb72cce
        │   └── hello_rust-741c35951fb72cce.d
        ├── examples
        ├── hello-rust
        ├── hello-rust.d
        └── incremental
            └── hello_rust-3g41msry4z82l

8 directories, 8 files

在这里插入图片描述

7. IDE/编辑器

IDE/编辑器在初学 rust 阶段是最不重要的事情。在这个阶段, 用 记事本/Vim 就可以完成 rust 代码的编写和运行。vim 也是没有安装任何插件的 vim, 只是开启了语法高亮:

~/.vimrc:

" let vim set indentation according to file type
if has('autocmd')
    filetype plugin indent on
endif

使用的操作系统是 Ubuntu 22.04, KDE 桌面。

8. References


  1. https://www.rust-lang.org/learn/get-started ↩︎

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/680269.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Jetson安装Anaconda(miniforge3)

1 miniforge3 miniforge集成了Anaconda的核心工具:conda。conda是一个包和环境管理工具。因此, miniforge里面的conda和Anaconda里面的conda完全一样;你能用Anaconda做的安装、升级、删除包等功能,miniforge都能做;你…

angular实现自定义模块路由懒加载;配置自定义模块路由及子路由

图片中绿色表示新建的文件;黄色表示被更改的文件; 1、创建一个新的项目 ng new angularlazyload2、创建一个用户模块,并配置路由 ng g module module/user --routing如图: 3 、在module/模块下创建user组件 ng g component module/user如图: 4、实现路由懒加载 依次…

java00——类和对象

在Java中一切皆对象 什么是对象? 一个人、一只猫、一条狗…这些就是一个对象; 每个对象都有属性和行为。 什么是类? 类即同类别,例如不论男人、女人、黑人、白人…,都是人类,即同一类事务的统称。 类的…

HTB-Sandworm

HTB-Sandworm 立足altas -> silentobserversilentobserver -> 完整的atalsatlas -> rootexploit 扫描最常用的1000个端口。 80会重定向到443。 去看看443有什么吧。 目录扫描可能不会起作用。在concat上面找到了一个有趣的东西。 “如果不知道怎么PGP&#xff1…

Axure教程—折叠面板

本文介绍利用Axure中的动态面板制作折叠面板 一、效果 预览地址:https://3k8az1.axshare.com 二、功能 1、点击标题展开面板内容 2、点击标题折叠面板 三、制作 从默认元件库拖入一个动态面板,设置两个状态,一个状态面板标题,一…

【MySQL】不允许你还不了解创建计算字段

🎬 博客主页:博主链接 🎥 本文由 M malloc 原创,首发于 CSDN🙉 🎄 学习专栏推荐:LeetCode刷题集! 🏅 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指…

【开源与项目实战:开源实战】82 | 开源实战三(中):剖析Google Guava中用到的几种设计模式

上一节课,我们通过 Google Guava 这样一个优秀的开源类库,讲解了如何在业务开发中,发现跟业务无关、可以复用的通用功能模块,并将它们从业务代码中抽离出来,设计开发成独立的类库、框架或功能组件。 今天,…

【后端】使用TS编写任务管理系统----Express

文章目录 常见的后端框架安装并且声明文件库项目基本配置编写任务管理后端API添加任务查看任务设置任务完成状态删除任务 总结 node -v v16.13.0https://github.com/dL-hx/server-side 常见的后端框架 expresskoa… 安装并且声明文件库 $ npm i express $ npm i types/exp…

前端vue入门(纯代码)14

内容创作不易,各位帅哥美女,求个小小的赞!!! 【15.给todoList案例添加编辑按钮】 本篇内容在TodoList案例的基础上添加个编辑按钮,要求: (1)点击编辑按钮后&#xff0c…

轻松学会研华屏幕下载和上传

🔥一个人走得远了,就会忘记自己为了什么而出发,希望你可以不忘初心,不要随波逐流,一直走下去🎶 🦋 欢迎关注🖱点赞👍收藏🌟留言🐾 ✅ 如果觉得博主…

HashMap和HashSet的知识点总结

前言 在之前我们介绍过TreeMap和TreeSet: TreeMapTreeSet 知识点梳理总结_Crystal_bit的博客-CSDN博客 也知道Key-Value和Key模型,但是我们可能还对Hash不太了解,这里我们对Hash了解之后再对HashMap和HashSet的基本使用了解一下。 目录 1…

LabVIEW 图像处理功能

设置成像系统并采集图像后,您可以分析和处理图像,以提取有关被检测对象的有价值信息。 内容 图像分析图像处理斑点分析机器视觉 图像分析 影像分析结合了基于影像像素的灰度强度计算统计数据和测量的技术。您可以使用影像分析功能来确定影像质量是否足以…

scratch lenet(10): C语言计算log

scratch lenet(10): C语言计算log 文章目录 scratch lenet(10): C语言计算log1. 目的2. 原理: x m ∗ 2 p x m * 2^p xm∗2p3. 公式展开3.1 公式分解3.2 获取 m m m3.3 获取 p p p3.4 Remez 算法Remez 算法用于 sin(x) 的快速计算Remez 算法用于 exp 的近似Remez 用于自然…

【MySQL数据库】存储过程

目录 一、存储过程1.1概述1.2优点 二、存储过程实战2.1创建存储过程2.2存储过程的参数2.3条件语句 if-then-else end if2.4循环语句while end while 一、存储过程 1.1概述 存储过程是一组为了完成特定功能的SQL语句集合。存储过程在使用过程中是将常用或者复杂的工作预先使…

CSS基础学习--25 border(边框)进阶

一、边框常见属性 border-radiusbox-shadowborder-image 属性说明CSSborder-image设置所有边框图像的速记属性。3border-radius一个用于设置所有四个边框- *-半径属性的速记属性3box-shadow附加一个或多个下拉框的阴影3 二、border-radius 圆角 在 CSS2 中添加圆角棘手。我…

网络协议TCP/IP 协议学习笔记一

T C P / I P通常被认 为是一个四层协议系统,每一层负责不同的功能: 1) 链路层,有时也称作数据链路层或网络接口层, 通常包括操作系统中的设备驱动程序和计算机 中对应的网络接口卡。它们一起处理与电缆(或其他任何传输…

CSS基础学习--26 渐变(Gradients)

CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡。以前,你必须使用图像来实现这些效果。但是,通过使用 CSS3 渐变(gradients),你可以减少下载的时间和宽带的使用。此外&…

Linux tracing之基于uprobe/kprobe的调试调优浅析

经过长期的发展, kprobes/uprobes 机制在事件(events)的基础上分别为内核态和用户态提供了追踪调试的功能, 这也构成了 tracepoint 机制的基础, 后期的很多工具, 比如 perf_events, ftrace 等都是在其基础上演化而来. 参考由 Brendan Gregg 提供的资料来看, kprobes/uprobes 在…

Minecraft我的世界服务器搭建之Linux系统,我的世界服务器推荐

Minecraft 是一个流行的沙箱独立游戏,由瑞典程序员 Markus “Notch” Perssion 首先创造,后来由 Mojang 开发并发布。这是一款关于打碎和放置砖块的游戏。首先,人们建造建筑物来抵抗夜晚的怪物,随着游戏的发展,玩家一起…