Rust安装与编写第一个rust程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cnwyt/article/details/84959550

Rust 是 Mozilla 开发的注重安全、性能和并发性的编程语言。

下边来演示一下如何安装rust,并尝试创建第一个rust项目。

使用 rustup 脚本安装:

第一步: 执行 curl https://sh.rustup.rs -sSf | sh, 下载安装器:

$ 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/wangtom/.cargo/bin

This path will then be added to your PATH environment variable by modifying the
profile files located at:
  /Users/wangtom/.profile
  /Users/wangtom/.zprofile
  /Users/wangtom/.bash_profile

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

第二步,命令行会提示安装选项,选择一个(1)默认安装选项:

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 2018-12-06, rust version 1.31.0 (abe02cefd 2018-12-04)
info: downloading component 'rustc'
 62.6 MiB /  62.6 MiB (100 %) 617.6 KiB/s ETA:   0 s
info: downloading component 'rust-std'
 49.0 MiB /  49.0 MiB (100 %) 682.7 KiB/s ETA:   0 s
info: downloading component 'cargo'
  3.6 MiB /   3.6 MiB (100 %) 609.3 KiB/s ETA:   0 s
info: downloading component 'rust-docs'
  8.5 MiB /   8.5 MiB (100 %) 559.0 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.31.0 (abe02cefd 2018-12-04)

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

如果屏幕上出现 Rust is installed now. Great!等字样, 就说明安装成功了。

尝试运行一下 cargo 等命令,却报命令不存在。可以看到,刚才上边的安装提示中,已经提示了PATH环境变量已经添加到.profile等文件了,为什么命令不存在呢?

$ which cargo
cargo not found
$ which rustc
rustc not found

查看 ~/.cargo/bin 目录,各个命令已经存在了:

$ ls -l ~/.cargo/bin 
total 132560
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 cargo
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 cargo-clippy
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 cargo-fmt
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rls
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rust-gdb
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rust-lldb
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rustc
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rustdoc
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rustfmt
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rustup

查看 ~/.bash_profile 文件,已经存在了命令导出:

export PATH="$HOME/.cargo/bin:$PATH"

重新加载一下 .bash_profile 配置即可:

$ source ~/.bash_profile

再次使用 cargorustc 等命令查看,已经可以正常找到路径了:

$ which cargo
/Users/wangtom/.cargo/bin/cargo
$ which rustc
/Users/wangtom/.cargo/bin/rustc

$ cargo --version
cargo 1.31.0 (339d9f9c8 2018-11-16)
$ rustc --version
rustc 1.31.0 (abe02cefd 2018-12-04)

可以看到,目前安装的是 rust 1.31.0 版本。

来一个HelloWorld程序

现在,已经安装好了 Rust,接下来让我们开始写第一个 Rust 程序。
按照惯例,当然是HelloWorld了。

Cargo, 是rust的构建工具,同时也是rust的包管理器。

使用 cargo new 命令创建一个项目,名为hello_world:

$ cargo new hello_world
     Created binary (application) `hello_world` package

直接进入 hello_world 目录,查看一下命令结果,生成了一个 Cargo.toml 文件和一个 main.rs 文件:

$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files

查看一下 Cargo.toml 文件:

[package]
name = "hello_world"
version = "0.1.0"
authors = ["wangtom"]
edition = "2018"

[dependencies]

打开 src/main.rs 文件, 其代码如下:

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

构建代码:

$ cargo build   
 Compiling hello_world v0.1.0 (/Users/wangtom/Code/rust/hello_world)
  Finished dev [unoptimized + debuginfo] target(s) in 2.53s 

执行构建后,会生成一个 target/debug 目录,里边有一堆文件,同时在项目根目录也生成了一个Cargo.lock文件,看起来和 node 的 package-lock.json 或者PHP的 composer.lock 类似。

运行构建结果:

$ ./target/debug/hello_world
Hello, world!

也可以直接使用 cargo run 命令,直接构建并执行构建结果:

$ cargo run
 Finished dev [unoptimized + debuginfo] target(s) in 0.08s 
  Running `target/debug/hello_world`
Hello, world!

使用 rustc 运行

创建一个 hello_world2 目录,并创建一个 main.rs:

$ mkdir hello_world2
$ cd hello_world2 
$ vi main.rs

main.rs 文件中,写下 Hello, world 程序:

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

使用 rustc 命令编译,然后可以直接执行编译结果,依然可以正常输出。

$ rustc main.rs 
$ ll
total 1152
drwxr-xr-x  4 wangtom  staff     128 12 11 18:10 ./
drwxr-xr-x  4 wangtom  staff     128 12 11 18:09 ../
-rwxr-xr-x  1 wangtom  staff  584444 12 11 18:10 main*
-rw-r--r--  1 wangtom  staff      46 12 11 18:10 main.rs
$./main 
Hello, world!

这个rustc命令看起来和Java里的javac命令作用一样。

参考链接

https://doc.rust-lang.org/cargo/guide/index.html

[END]

猜你喜欢

转载自blog.csdn.net/cnwyt/article/details/84959550