关于rust:初窥门径从hello-world开始rust学习

6次阅读

共计 934 个字符,预计需要花费 3 分钟才能阅读完成。

你好,我是鹿洺。从本文开始,我将和你一起学习 rust。在开始之前,你须要实现 rust 工具链的装置。

工欲善其事必先利其器,你能够应用任何编辑器来写 rust 代码 ,我比拟喜爱 VSCode,它收费,功弱小而且速度很快。在 VSCode 下我装置了一些插件来提供效率,你有趣味也能够参考:

  • rust-analyzer:会实时编译和剖析你的 rust 代码,提醒代码中的谬误,并对类型进行标注
  • rust syntax:为你的 rust 代码提供语法高亮
  • creates:帮忙你剖析以后我的项目的依赖是否是最新的版本
  • better toml:rust 应用 toml 做我的项目的配置管理,它能够帮你语法高亮,并展现 toml 文件中的谬误
  • rust test lens:能够帮忙你疾速运行某个 rust 测试
  • tabnine:基于 AI 的主动补全,能够帮忙你更快撰写代码

当初,依照传统,咱们入手来写第一个 rust 程序。咱们在本地命令行应用 cargo new 来创立我的项目。

> cargo new hello
     Created binary (application) `hello` package

此时,会主动帮咱们创立一个可执行的我的项目 hello,入口在 src/main.rs,我的项目配置在 Cargo.toml 文件里。

// 我的项目入口
fn main() {println!("Hello, world!");
}
[package]
name = "hello"
version = "0.1.0"
edition = "2018"

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

[dependencies]

进入这个我的项目目录,咱们运行 cargo run,通过编辑后,程序开始运行。

> cd .\hello\
> cargo run
Compiling hello v0.1.0 (F:\projects\start-rust\hello)
    Finished dev [unoptimized + debuginfo] target(s) in 1.67s
     Running `target\debug\hello.exe`
Hello, world!

咱们的第一个 rust 程序运行胜利!

正文完
 0