惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News | PayPal Newsroom
Application and Cybersecurity Blog
Application and Cybersecurity Blog
O
OpenAI News
S
Security @ Cisco Blogs
宝玉的分享
宝玉的分享
Hacker News: Ask HN
Hacker News: Ask HN
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Latest news
Latest news
NISL@THU
NISL@THU
S
Security Affairs
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
AI
AI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
小众软件
小众软件
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
AWS News Blog
AWS News Blog
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
I
InfoQ
Schneier on Security
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
IT之家
IT之家
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
H
Heimdal Security Blog
S
SegmentFault 最新的问题

文章列表 on polarisxu

通过一个例子让你彻底掌握 Go 工作区模式 这道题正确率竟然只有 22% Go 开源项目推荐:一个简单的 Go 练手项目 go test 如何禁用缓存? Go1.18 快讯:constraints 包被移除标准库 跟着 Go 作者学泛型 「2022 版」轻松搞定 Go 开发环境 鹅厂小哥用行动反对加班:程序员加班能被终结吗? Go 为什么选择 Gopher 作为吉祥物? 本地如何配置多个 GitHub/Gitee 账号? 为 Java 开发者准备的 Go 教程 02:Java 有而 Go 无 GitHub 发现了 studygolang 项目依赖的漏洞 Typora 宣布收费后,这款开源 Markdown 编辑器火了 为 Java 开发者准备的 Go 教程 01:漫游了 我的 2021 年终总结 泛型版 singleflight:Go 中如何防止缓存击穿? 2021 年你写了多少代码?这个 Go 工具帮你统计 Go1.18 新特性:TryLock Go 泛型入门教程 程序员瑞士军刀:各种结构的转换工具 终于有 Go 版的 Elasticsearch 了 Go泛型系列:maps 包讲解 Go 1.18 中的 any 是什么? Go1.18 快讯:新的 IP 包 Go编程模式:详解函数式选项模式 Go泛型系列:slices 包讲解 Nginx 竟然也有 playground — Go 语言构建的 Go泛型系列:Go1.18 类型约束那些事 Go1.18 快讯:Module 工作区模式 Go1.18 快讯:新增的 Cut 函数太方便了 Go1.18 快讯:废弃了这个 API Go1.18 快讯:新增字符串 Clone API Go:如何获得项目根目录? Go 如何获取和设置环境变量 重磅变动:更快找到微信公众号里的Go文章 Go 这样设置版本号:我们的项目也可以 假期结束,推荐 2 本 manning 出的 Go 图书 Go Fiber 框架系列教程 04:测试应用 Go Fiber 框架系列教程 03:中间件 Go泛型系列:提前掌握Go泛型的基本使用 推荐一个 Go GUI 实战项目 Go Fiber 框架系列教程 02:详解相关 API 的使用 Go Fiber 框架系列教程 01: 和 Express 对比学习 Go Module:私有不合规库怎么解决引用问题 Go1.17 新特性:go get 变了 Go1.17 新特性:testing 包的相关变化 StackOverflow 上关于 Go select 死锁的问题 新书推荐:用 Gin 框架构建分布式应用 Go 官网要变天。。。 Gin 这是要成为 Go 官方框架? 扬眉吐气:刚刚,Go 已经默认支持泛型了 Go 1.17 新特性:Module 有哪些变化? Go 第三方库推荐:类型转换如此简单 担心密码提交到 GitHub?建议使用这个 Go 开源工具 Go 的时间格式化为什么是 2006-01-02 15:04:05? GitHub 为 Go 社区带来安全支持 Go开源项目推荐:500行代码确认请求时间花在哪 一道关于 len 函数的诡异 Go 面试题解析 厉害了我的 Go:推荐系统都有开源实现 Go1.17 新特性:新版构建约束 这个工具真好:看看你的Go项目依赖有无漏洞 网友很强大,发现了Go并发下载的Bug Rust 劝退系列 09:函数 Go项目实战:一步步构建一个并发文件下载器 这本 Go 新书挺期待的:100 个常见错误 Go 启用新的官方问答社区 这是要干嘛?!微软招 Go 编译器全职开发人员 Go1.17 快报之标准库越来越注重易用性 Go1.17 新特性之切片变数组 回顾 Go 官网的演变史 站长8年前的Go代码竟然进入大厂的项目里了 Go图书翻译:一个好消息,一个坏消息 不怕烂尾!决定组织翻译这本 Go 图书 Rust 劝退系列 08:模式匹配 一本花了2.5年写成的Go免费在线图书 被黑惨了:一句话,说明自己会 Go,咋整? Go1.16 中的新函数 signal.NotifyContext 怎么用? Go 1.17 新特性提前学之测试执行随机化 Rust 劝退系列 07:流程控制 官方的 Go 多版本管理:使用和原理 Rust新书:给你一个劝退的理由 周刊题解:常量表达式这个规则应该了解下 盘点那些使用 Go 语言的国外公司 编写了50万行Go代码是一种什么体验 Rust 劝退系列 06:常量 Uber 使用 Go 的规模这么大?!都自己定制的 Go 编译器了 Go Team Leader — rsc 大神新开源了一个库,增强模板功能 再一次看到了 Go 的节制:int128 类型要不要支持? Rust 劝退系列 05:复合数据类型 「卷」有理论依据:海勒姆定律—Go又是怎么卷的 Go 真的也可以进行 GUI 开发:还有这样的图书呢 Rust 劝退系列 04:基本数据类型 我的 Go 语言书单 Rust 劝退系列 03:变量 我又来推荐免费 Go 新书了:一本用 Go 讲架构的书 注释竟然还有特殊用途?一文解惑 //go:linkname 指令 这个功能,公众号、微信群会不会被玩坏? 一道 Go 闭包题,面试官说原来自己答错了:面别人也涨知识 Rust 劝退系列 01:打造开发环境 答应我,这次一定彻底搞懂 Go 中的类型别名
Rust 劝退系列 02:第一个 Rust 程序
2021-04-11 · via 文章列表 on polarisxu

大家好,我是站长 polarisxu。

上节我们准备好了 Rust 环境,现在开始我们的第一个 Rust 程序。

01 Hello World

在终端执行如下命令,创建相关目录结构:

$ mkdir ~/rust-learning
$ cd ~/rust-learning
$ mkdir hello
$ cd hello

启动 VSCode,在 Welcome 页面选择打开文件夹:~/rust-learning,在 hello 目录下创建 main.rs 文件,输入如下代码:

细心的读者可能看到了 fn 下面的三个点,鼠标放上去提示:

file not included in module tree rust-analyzer(unlinked-file)

这个提示的详细说明见链接:https://rust-analyzer.github.io/manual.html#unlinked-file。大意是,当前文件不在任何 crate 中。也就是说,rust-analyzer 插件要求标准的 cargo 目录。因此,编译运行该程序得通过终端:

$ rustc main.rs
$ ./main
Hello World!

这就是你的第一个 Rust 程序。关于 rustc,目前我们只需要知道它是 Rust 的编译器即可,因为实际中很少直接使用它。

02 cargo 版 Hello World

上文说,编译 Rust 项目,很少直接使用 rustc 这个 Rust 编译器,那应该用什么呢?

简单的程序(比如上面的单个文件)可能偶尔会用 rustc,但绝大部分时候,特别是项目中,我们应该一直使用 Rust 的生成工具和依赖管理器 Cargo。

现在就介绍下 Cargo 这个工具。

Cargo

在安装完 rustup 后,Rust 工具链会默认安装上,这包括 Cargo 这个工具。

因为这个工具很重要,官方直接提供了一个 Cargo 的手册:https://doc.rust-lang.org/cargo/index.html

Cargo 是 Rust 的包管理器。它能够下载 Rust 包的依赖,编译包,制作可分发的包,并将它们上传到 crates.io 上(这是 Rust 社区的包注册中心)。

这个手册内容较多,有兴趣可以通读下,后续也可以用于查阅。我们这里只介绍经常使用的相关命令。先看一眼 Cargo 命令的相关帮助:

$ cargo
Rust's package manager

USAGE:
    cargo [+toolchain] [OPTIONS] [SUBCOMMAND]

OPTIONS:
    -V, --version           Print version info and exit
        --list              List installed commands
        --explain <CODE>    Run `rustc --explain CODE`
    -v, --verbose           Use verbose output (-vv very verbose/build.rs output)
    -q, --quiet             No output printed to stdout
        --color <WHEN>      Coloring: auto, always, never
        --frozen            Require Cargo.lock and cache are up to date
        --locked            Require Cargo.lock is up to date
        --offline           Run without accessing the network
    -Z <FLAG>...            Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
    -h, --help              Prints help information

Some common cargo commands are (see all commands with --list):
    build, b    Compile the current package
    check, c    Analyze the current package and report errors, but don't build object files
    clean       Remove the target directory
    doc         Build this package's and its dependencies' documentation
    new         Create a new cargo package
    init        Create a new cargo package in an existing directory
    run, r      Run a binary or example of the local package
    test, t     Run the tests
    bench       Run the benchmarks
    update      Update dependencies listed in Cargo.lock
    search      Search registry for crates
    publish     Package and upload this package to the registry
    install     Install a Rust binary. Default location is $HOME/.cargo/bin
    uninstall   Uninstall a Rust binary

See 'cargo help <command>' for more information on a specific command.

常用的命令如下:

  • 使用 cargo new 创建新的 package(包),包括可执行的和普通包。
  • 使用 cargo build 构建你的包。
  • 使用 cargo run 生成和运行包。
  • 使用 cargo test 测试你的包。
  • 使用 cargo check 进行包分析,并报告错误。
  • 使用 cargo doc 为你的包(以及依赖包)生成文档。
  • 使用 cargo publish 将包发布到 crates.io。
  • 使用 cargo install 安装 Rust 可执行程序。

借助 VSCode,很多命令不需要我们手动输入执行。

注:帮助说明中,都是 package,你可以理解成项目,下文有时会用项目的说法。

Cargo 使用

使用 Cargo 创建一个新项目:

$ cd ~/rust-learning
$ cargo new hello-cargo
     Created binary (application) `hello-cargo` package

生成了 hello-cargo 目录,其中包含如下文件:

$ tree hello-cargo
hello-cargo
├── Cargo.toml
└── src
    └── main.rs

其中 Cargo.toml 是 Rust 的清单文件(toml 格式,这种格式和 ini 文件有点像,但更强大),用于保存项目和依赖的元数据信息,类似 Go Module 中的 go.mod 文件。看下其中的内容:

[package]
name = "hello-cargo"
version = "0.1.0"
authors = ["xuxinhua <polaris@studygolang.com>"]
edition = "2018"

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

[dependencies]

其中,edition 字段目前可以是 2015 和 2018,默认是 2018,具体什么区别,可以认为 2018 是一个 Rust 大版本(虽然向下兼容)。

关于 authors 字段怎么获取的,可以参考 https://doc.rust-lang.org/cargo/commands/cargo-new.html,里面有详细的解释。(也可以通过 cargo help new 帮助中查到)

因为我们的项目还没有依赖,所以 dependencies 项是空的。

除了看到的文件,Cargo 还生成了 git 相关的隐藏文件和文件夹:.git.gitignore。也就是说,默认情况下,该项目就通过 git 进行版本控制,可以通过 --vcs 选项控制。

最后是 Rust 源代码。Cargo 要求,源代码必须在 src 目录下,现在 main.rs 中就是一个简单的 Hello World:

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

在 VSCode 中打开会发现顶部多出了两个按钮:

点就 「Run」,会在 VSCode 下方 Terminal 窗口输出类似如下信息:

> Executing task: cargo run --package hello-cargo --bin hello-cargo <

   Compiling hello-cargo v0.1.0 (/Users/xuxinhua/rust-learning/hello-cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 1.61s
     Running `target/debug/hello-cargo`
Hello, world!

Terminal will be reused by tasks, press any key to close it.

这里用的是 cargo run 这个命令:先编译,显示编译完成相关信息,然后运行。--package 指定要运行的目标包名, --bin 指定要运行的目标二进制文件名。(实际上,针对当前 hello-cargo 项目,执行运行 cargo run 效果是一样的)

这时,项目根目录会生成一个 target 目录,你可以 tree target 看看,里面的文件很多,具体每个文件的作用我现在也不知,一般也不用去知晓,别劝退~

你可以在终端输入如下命令:

$ cargo run --release

   Compiling hello-cargo v0.1.0 (/Users/xuxinhua/rust-learning/hello-cargo)
    Finished release [optimized] target(s) in 1.28s
     Running `target/release/hello-cargo`
Hello, world!

生产环境运行的程序应该始终使用 --release 选项。这时,在 target 目录下会生成一个 release 目录,而不是 debug 目录。

在上面和 「Run」一起的还有一个 「Debug」按钮。点击它会提示你需要安装 CodeLLDBMS C++ tools 扩展。本系列后续介绍调试相关知识。

03 小结

从上面的介绍中可以看到,cargo 命令很强大。Go 语言中,基本上一个 go 命令搞定一切(这也是很多人觉得 Go 工具链很强大的原因)。而 Rust 中,你基本上不需要知道 rustc 命令的存在,只需要掌握 cargo 即可。话说,为啥叫 cargo 呢?crate + go?让人以为和 Go 语言相关呢。。。

本节的 Hello World 应该不会劝退,而且可能会喜欢上它。不过我们没有对 Rust 语法做任何讲解,有些人看到 Hello World 的代码可能有一些奇怪,包括 fn、println!。下一节开始,就该讲解 Rust 的语法了。