你好,我是鹿洺。从本文开始,我将和你一起学习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 runCompiling 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程序运行胜利!