分类
dev

Rust-Lang Notes

Rust语言学习笔记

Installation 安装

习惯的

$ brew search rust
==> Searching local taps...
rust       rustc-completion       rustup-init      uncrustify

然后顺手就可以:

$ brew install rust

这样安装完之后是可以使用rustc等的.

然而缺少rustup工具, 也因为brew同一管理路径的原因, 缺失~/.cargo目录, (不知道为啥没有至少一个软连接?).

所以出于学习阶段(在我还没弄清谁更好时), 先使用官方的方法(况且cargo是Rust的package manager):

如果brew安装过的, 别忘了先brew uninstall rust卸载掉.

$ curl https://sh.rustup.rs -sSf | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust programming
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin
directory, located at:

  /Users/blodely/.cargo/bin

This path will then be added to your PATH environment variable by modifying the
profile files located at:

  /Users/blodely/.profile
  /Users/blodely/.zprofile

You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

Current installation options:

   default host triple: x86_64-apple-darwin
     default toolchain: stable
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
1

info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2017-08-31, rust version 1.20.0 (f3d6973f4 2017-08-27)
info: downloading component 'rustc'
 35.1 MiB /  35.1 MiB (100 %) 121.6 KiB/s ETA:   0 s
info: downloading component 'rust-std'
 49.0 MiB /  49.0 MiB (100 %) 128.0 KiB/s ETA:   0 s
info: downloading component 'cargo'
  2.6 MiB /   2.6 MiB (100 %) 124.8 KiB/s ETA:   0 s
info: downloading component 'rust-docs'
  3.6 MiB /   3.6 MiB (100 %) 115.2 KiB/s ETA:   0 s
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
info: default toolchain set to 'stable'

  stable installed - rustc 1.20.0 (f3d6973f4 2017-08-27)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env

这里当然猴急的运行source $HOME/.cargo/env,

再就测试一下:

$ rustc --version
rustc 1.20.0 (f3d6973f4 2017-08-27)

一切正常.

Hello, world

$ vim main.rs

然后录入:

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

编译:

$ rustc main.rs

运行二进制文件:

$ ./main
Hello, world!~

Cargo

Cargo命令给出的解释是: Rust's package manager.

好吧, 使用Cargo来创建一个工程看看.

$ cargo new hello_cargo --bin
    Created binary (application) `hello_cargo` project

这样就创建了一个binary即应用程序(而不是一个库)的工程了.

cd进去发现, 还给默认初始化成一个git仓库了.

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .gitignore
    Cargo.toml
    src/

文件结构已归好, 还增加git ignore文件.

查阅文档, --vcs参数可控制此项.

然后这个Cargo.toml文件类型让我为之一震...

toml = Tom's Obvious, Minimal Language GithubRepo

$ cat Cargo.toml
[package] # 段落标题; 配置一个包
name = "hello_cargo"
version = "0.1.0"
authors = ["Luo Yu <indie.luo@gmail.com>"]

[dependencies] # 项目依赖的crates列表(crate: Rust代码包)

构建与运行:

$ cargo build # cargo run
   Compiling hello_cargo v0.1.0 (file:///Users/blodely/Desktop/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 3.27 secs
$ ./target/debug/hello_cargo
Hello, world!

Author

Luo Yu

indie.luo@gmail.com

Tuesday, September 12, 2017

Rick Luo

indie designer & developer.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据