来自简书
介绍Rust
Rust 是由Mozilla主导开发的专门用来编写高性能应用程序的系统级编程语言, 也是一门强调安全、并发、高效的语言。 Graydon Hoare从2006年开发Rust,之后Mozilla对Rust很感兴趣,并把Graydon吸收到Mozilla社区。在大家的一番努力下,终于在2015年5月份发布了 Rust 正式版 v1.0.0。
安装
使用安装脚本快速安装,从之后踩得坑来看,mac 系统 、Linux、Win Subsystem 只推荐使用这种方式安装:
curl https://sh.rustup.rs -sSf | sh
如何查看版本呢?
rustc -V
不想安装Rust 环境呢?
Rust在线运行工具play-rust 不会让你上当,就看你本地的网络争不争气了。
rust 起手式
//hi.rs
fn main(){
println!("hello rust")
}
编译执行
rustc hi.rs #编译出 hi执行文件
./hi #执行
C / Golang / Rust 分别打印hello world,看下它们编译出的文件大小。
61B hi-c.c
8.3K hi-c
72B hi-go.go
1.1M hi-go
39B hi.rs
477K hi-rust
rust的477K比C的8K大不少,这里要吐槽下Go了,Go 默认静态编译足足有1.1M啊。不过比起 Rust的编译时间,Go这点硬盘空间又算得了什么呢。
Rust Cargo
Car-go 是Rust的包管理工具,集成了 下载/管理依赖/测试/编译,跟npm pip bundler 作用类似,使用Cargo没有Make 、CMake带来的那种尴尬,不会被不知所云的Makefile折磨的死去活来。这点上 Go的包管理工具快快受死吧。
查看cargo的版本 cargo -V,在安装rust的时候,cargo也一并安装了。
使用 cargo新建个项目
cargo new hyper --bin
hyper
├── Cargo.toml # 项目配置文件, 类似npm的pakage.json
└── src
└── main.rs
Cargo.toml 使用Toml文件配置。 Toml 是一种极简的配置文件格式。
# Cargo.toml
[package]
name = "hyper"
version = "0.0.1"
authors = ["shooter"]
[dependencies]
time = "0.1.12"
regex = "0.1.41"
rocket = "0.3.16"
编译
cargo build
hyper
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
└── debug
├── build
├── deps
├── examples
├── hyper
├── hyper.d
├── incremental
└── native
更新
cargo update
执行
cargo run
Cargo跟 ruby-bundler有很多相似之处,Rust 最初的包管理项目失败后, Mozilla 请了 Yehuda Katz 和 Carl Lerche 开发了Cargo。他们正是Bundler的主要作者。
请看下一章 介绍Rust(2): 基本数字类型
参考: crates.io/
www.rust-lang.org/en-US/insta…
zhuanlan.zhihu.com/time-and-sp… 某Rust大佬的知乎专栏