美文网首页
介绍Rust(1): 安装与包管理工具

介绍Rust(1): 安装与包管理工具

作者: 已不再更新_转移到qiita | 来源:发表于2018-09-07 18:34 被阅读139次
Rust

介绍Rust

Rust 是由Mozilla主导开发的专门用来编写高性能应用程序的系统级编程语言, 也是一门强调安全、并发、高效的语言。
Graydon Hoare从2006年开发Rust,之后Mozilla对Rust很感兴趣,并把Graydon吸收到Mozilla社区。在大家的一番努力下,终于在2015年5月份发布了 Rust v1.0.0正式版。

安装

使用安装脚本快速安装,从之后踩得坑来看,只推荐使用这种方式。
curl https://sh.rustup.rs -sSf | sh

查看版本
rustc -V

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足足有1.1M啊。

Rust Cargo

Car-go 是Rust的包管理工具,集成了 下载依赖/管理依赖/测试/编译, 跟npm pip bundler 作用类似,使用Cargo没有Make 、CMake带来的那种尴尬,不会被众多的makefile折磨的死去活来。

查看cargo的版本 cargo -V,安装rust的时候,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 KatzCarl Lerche 开发了Cargo。他们正是Bundler的主要作者。


参考:

https://zhuanlan.zhihu.com/time-and-spirit-hut 某大神的Rust知乎专栏
https://www.rust-lang.org/en-US/install.html
http://crates.io/
http://wiki.jikexueyuan.com/project/rust-primer/cargo-projects-manager/cargo-projects-manager.html

相关文章

网友评论

      本文标题:介绍Rust(1): 安装与包管理工具

      本文链接:https://www.haomeiwen.com/subject/qlwbwftx.html