Rust 的安装和使用举例
一、环境
二、安装
$curl -sSf https://static.rust-lang.org/rustup.sh | sh
Welcome to Rust.This script will download the Rust compiler and its package manager, Cargo, and
install them to /usr/local. You may install elsewhere by running this script
with the --prefix=<path> option.The installer will run under 'sudo' and may ask you for your password. If you do
not want the script to run 'sudo' then pass it the --disable-sudo flag.You may uninstall later by running /usr/local/lib/rustlib/uninstall.sh,
or by running this script again with the --uninstall flag.Continue? (y/N) yrustup: gpg not available. signatures will not be verified
rustup: downloading manifest for 'stable'
rustup: downloading toolchain for 'stable'
######################################################################## 100.0%
rustup: extracting installer
rustup: installing toolchain for 'stable'
Password:
install: creating uninstall script at /usr/local/lib/rustlib/uninstall.sh
install: installing component 'rustc'
install: installing component 'rust-std-x86_64-apple-darwin'
install: installing component 'rust-docs'
install: installing component 'cargo'Rust is ready to roll.
三、查看
$rustc --version
rustc 1.7.0 (a5d1e7a59 2016-02-29)
$cargo --version
cargo 0.8.0-nightly (28a0cbb 2016-01-17)
四、创建项目文件夹结构
$cd ~
$mkdir develop
$cd develop/
$mkdir rust-projects
$cd rust-projects/
$mkdir hello_world
$cd hello_world/
五、rust 之 helloworld
$vi main.rs
fn main() {println!("Hello, world!");
}
$rustc main.rs
$./main
Hello,world!
六、cargo 之 helloworld
当前文件夹在 hello_world
1、主要的cargo 命令行
$mkdir src
$mv main.rs src/main.rs
$rm main
$vi Cargo.toml
Cargo.toml的内容例如以下
[package]
name = "hello_world"
version = "0.0.1"
authors = ["teamlet@email.com"]
$cargo build
Compiling hello_world v0.0.1 (file:///Users/teamlet/develop/rust-projects/hello_world)
$./target/debug/hello_world
Hello,world!
$cargo run
Running `target/debug/hello_world`Hello,world!
$cargo build --release
Compiling hello_world v0.0.1 (file:///Users/teamlet/develop/rust-projects/hello_world)
$cat Cargo.lock
[root]
name = "hello_world"
version = "0.0.1"
2、cargo项目的管理工具
1) 自己主动生成项目
$cd ..
$mkdir cargo_test
$cd cargo_test/
$cargo new hello_world --bin
$cd hello_world/
$cat Cargo.toml //查看cargo自己主动生成的配置文件
[package]
name = "hello_world"
version = "0.1.0"
authors = ["teamlet <teamlet@mail.com>"][dependencies]
$cd src/
$cat main.rs //查看cargo自己主动生成的helloworld代码
fn main() {println!("Hello, world!");
}
2)猜数游戏
$cd ..
$cargo new guessing_game --bin
$cd guessing_game/
$cargo build
Compiling guessing_game v0.1.0 (file:///Users/teamlet/develop/rust-projects/guessing_game)
$cargo run
Running `target/debug/guessing_game`Hello, world!
$vi src/main.rs
use std::io;fn main() {println!("Guess the number!");println!("Please input your guess.");let mut guess = String::new();io::stdin().read_line(&mut guess).expect("Failed to read line");println!("You guessed:{}",guess);
}
$cargo run
Compiling guessing_game v0.1.0 (file:///Users/teamlet/develop/rust-projects/guessing_game)Running `target/debug/guessing_game`
Guess the number!
Please input your guess.
5
You guessed:5
$vi Cargo.toml //添加 dependencies
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["teamlet <teamlet@mail.com>"][dependencies]rand = "^0.3.13"
$cargo build
Updating registry `https://github.com/rust-lang/crates.io-index`Downloading rand v0.3.13Downloading winapi-build v0.1.1Downloading advapi32-sys v0.1.2
假设出现
unable to get packages from source
则须要多次重复运行 cargo build 或者 cargo build -verbose,直到
Downloading winapi v0.2.5Compiling libc v0.2.8Running `rustc /Users/teamlet/.cargo/registry/src/github.com-88ac128001ac3a9a/libc-0.2.8/src/lib.rs --crate-name libc --crate-type lib -g --cfg feature=\"default\" --cfg feature=\"use_std\" -C metadata=c1044b0a546bbfd6 -C extra-filename=-c1044b0a546bbfd6 --out-dir /Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --emit=dep-info,link -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --cap-lints allow`Compiling winapi v0.2.5Running `rustc /Users/teamlet/.cargo/registry/src/github.com-88ac128001ac3a9a/winapi-0.2.5/src/lib.rs --crate-name winapi --crate-type lib -g -C metadata=96db160368c72f00 -C extra-filename=-96db160368c72f00 --out-dir /Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --emit=dep-info,link -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --cap-lints allow`Compiling winapi-build v0.1.1Running `rustc /Users/teamlet/.cargo/registry/src/github.com-88ac128001ac3a9a/winapi-build-0.1.1/src/lib.rs --crate-name build --crate-type lib -g -C metadata=7bc4b8a4c9d61577 -C extra-filename=-7bc4b8a4c9d61577 --out-dir /Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --emit=dep-info,link -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --cap-lints allow`Compiling advapi32-sys v0.1.2Running `rustc /Users/teamlet/.cargo/registry/src/github.com-88ac128001ac3a9a/advapi32-sys-0.1.2/build.rs --crate-name build_script_build --crate-type bin -g --out-dir /Users/teamlet/develop/rust-projects/guessing_game/target/debug/build/advapi32-sys-911258561df3b2a9 --emit=dep-info,link -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --extern build=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps/libbuild-7bc4b8a4c9d61577.rlib --cap-lints allow`Running `/Users/teamlet/develop/rust-projects/guessing_game/target/debug/build/advapi32-sys-911258561df3b2a9/build-script-build`Running `rustc /Users/teamlet/.cargo/registry/src/github.com-88ac128001ac3a9a/advapi32-sys-0.1.2/src/lib.rs --crate-name advapi32 --crate-type lib -g -C metadata=911258561df3b2a9 -C extra-filename=-911258561df3b2a9 --out-dir /Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --emit=dep-info,link -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --extern winapi=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps/libwinapi-96db160368c72f00.rlib --cap-lints allow`Compiling rand v0.3.13Running `rustc /Users/teamlet/.cargo/registry/src/github.com-88ac128001ac3a9a/rand-0.3.13/src/lib.rs --crate-name rand --crate-type lib -g -C metadata=340832a8942cb900 -C extra-filename=-340832a8942cb900 --out-dir /Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --emit=dep-info,link -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --extern libc=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps/liblibc-c1044b0a546bbfd6.rlib --extern advapi32=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps/libadvapi32-911258561df3b2a9.rlib --extern winapi=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps/libwinapi-96db160368c72f00.rlib --cap-lints allow`Compiling guessing_game v0.1.0 (file:///Users/teamlet/develop/rust-projects/guessing_game)Running `rustc src/main.rs --crate-name guessing_game --crate-type bin -g --out-dir /Users/teamlet/develop/rust-projects/guessing_game/target/debug --emit=dep-info,link -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug -L dependency=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps --extern rand=/Users/teamlet/develop/rust-projects/guessing_game/target/debug/deps/librand-340832a8942cb900.rlib`
改动代码,生成随机数
$vi src/main.rs
extern crate rand;use std::io;
use rand::Rng;fn main() {println!("Guess the number!");let secret_number = rand::thread_rng().gen_range(1, 101);println!("The secret number is: {}", secret_number);println!("Please input your guess.");let mut guess = String::new();io::stdin().read_line(&mut guess).expect("failed to read line");println!("You guessed: {}", guess);
}
$cargo build
Compiling guessing_game v0.1.0 (file:///Users/teamlet/develop/rust-projects/guessing_game)
$cargo run
Running `target/debug/guessing_game`Guess the number!The secret number is: 55Please input your guess.4You guessed: 4
參考文章地址:
https://doc.rust-lang.org/book/getting-started.html
https://doc.rust-lang.org/book/guessing-game.html
转载于:https://www.cnblogs.com/clnchanpin/p/7389499.html
Rust 的安装和使用举例相关推荐
- Rust——Macos安装使用
进入官网会自动检测当前是什么操作系统,我的是Mac,所以使用官网给的命令安装就可以了 终端输入: curl --proto '=https' --tlsv1.2 -sSf https://sh.rus ...
- mingw64+Rust+Windows11安装
Windows+mingw64+Rust安装,找了好久.奈何没完整的教程,自己写个吧... 官方推荐(但是我不太喜欢VS) 庞大无比(doge) 本文提供给不想安装庞大无比的微软C++库,并且之前又是 ...
- Rust自定义安装路径
1.下载安装包 下载地址:Install Rust - Rust Programming LanguageA language empowering everyone to build reliabl ...
- CentOS安装yum 镜像 举例阿里云镜像
如何安装yum 镜像 CentOS 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup ...
- linux编译blas,CBLAS编译安装与使用举例
在Github上看到有人用BLAS library优化自己的源码,对此产生了强烈兴趣. 准备自己动手实践一下,网上搜索了一大堆编译安装BLAS教程的资料,没一个靠谱的,编译过程中遇到一堆的问题.因为自 ...
- 在虚拟机中,安装Windows10各种语言版本安装详细步骤-举例韩语的ISO安装详细过程
根据之前的文章–VMware16安装Win10系统 执行到这里的步骤之后, (1)找到win10 iso的安装文件"Windows 10 x64.vmx" 删除行: firewar ...
- linux rust 卸载,linux宝塔面板安装rust
可能有些童鞋会用到rust,本文介绍下如何在linux宝塔面板环境安装rust,额,linux环境下安装都一样,不过很多人喜欢用宝塔然后搭配这些语言环境来使用. 如果你还没有宝塔看这里(宝塔服务器面板 ...
- lts安装 rust ubuntu_一起学Rust编程「1」:开发环境
引言 Rust是近几年获得广泛关注和认可的一门系统级编程语言.它严苛的静态类型检查和独特的所有权系统,使得编译器能够尽可能的帮开发者在编译时就排除一些符合常见模式的bug.这也让很多人认为rust是一 ...
- ubuntu安装 rust nightly_一起学Rust编程「1」:开发环境
引言 Rust是近几年获得广泛关注和认可的一门系统级编程语言.它严苛的静态类型检查和独特的所有权系统,使得编译器能够尽可能的帮开发者在编译时就排除一些符合常见模式的bug.这也让很多人认为rust是一 ...
- Ubuntu:安装rust
Ubuntu:安装rust 1.安装curl sudo apt install curl 安装速度慢的话可以参考https://blog.csdn.net/cacique111/article/det ...
最新文章
- 谁说数学不好,就不能成为编程大佬
- Sublime Text 全程指引 by Lucida
- python 打包发布网站_Python代码的打包与发布
- 通过 Serverless 加速 Blazor WebAssembly
- leecode11 盛水最多的容器
- linux6.5怎样安装vim,在Centos 6.5下成功安装和配置了vim7.4
- Java中的servlet是什么?
- 《增长黑客》节选与笔记
- Relab Sonsig Rev-A 演示 华丽的混响插件
- CST常用应用的求解器
- java 串行_java串行化
- 分享一下微带天线的心得体会
- 今日头条改版,搜索比重上升,占首屏三分之一
- vim的下载及配置安装
- 线性代数之 Ax=b反问题的一个特解
- 【云原生 | Kubernetes 实战】18、K8s 安全实战篇之 RBAC 认证授权(上)
- 中国大学MOOC货币金融学试题及答案
- 抓取前程无忧招聘信息
- 用计算机计算根号2 2-1,如何计算根号2?
- GNU make使用(一)