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) y rustup: 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.13
Downloading winapi-build v0.1.1
Downloading advapi32-sys v0.1.2
假设出现
unable to get packages from source
则须要多次重复运行 cargo build 或者 cargo build -verbose,直到
Downloading winapi v0.2.5
Compiling libc v0.2.8
Running `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.5
Running `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.1
Running `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.2
Running `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.13
Running `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: 55
Please input your guess.
4
You guessed: 4
參考文章地址:
https://doc.rust-lang.org/book/getting-started.html
https://doc.rust-lang.org/book/guessing-game.html
Rust 的安装和使用举例的更多相关文章
- 【Rust】Rust的安装和配置
-----------------------参考文档------------------------------------- https://www.rust-lang.org/tools/ins ...
- Rust 指定安装目录
集群home目录被管理员限制了存储空间,rust安装要100多M,默认安装home目录下,查了一圈,没找到rust指定安装目录的办法. 这里记录下解决办法: 在想要安装的目录执行 mkdir -p c ...
- 【rust】rust安装,运行第一个Rust 程序 (1)
安装 Rust 在 Unix 类系统如 Linux 和 macOS 上,打开终端并输入: curl https://sh.rustup.rs -sSf | sh 回车后安装过程出现如下显示: info ...
- Rust安装-运行第一个程序-hello_world
Rust官网:https://rust-lang.org/ 安装 点击install,选择版本 选择相对应的版本进行下载 我这里下载的是windows系统,运行下载好的exe文件,根据需要选择选对应的 ...
- 不用rustup,Windows下gnu版Rust安装与开发环境配置
写在前面 本文介绍了在不使用rustup的情况下,在Windows上安装gnu版的Rust,并配置开发环境(VSCode + rust-analyzer,CLion + IntelliJ Rust)的 ...
- 【MySQL】MySQL无基础学习和入门之二:MySQL的安装
安装MySQL安装一般分为源码包编译安装.分发包.rpm包安装和yum安装,四种安装方式有一些区别,对应的适用场景也不一样. 源码包:源码包就是程序源代码包,其中包含程序代码文件,这些代码文件是文本型 ...
- 安装成功的nginx如何添加未编译安装模块
原已经安装好的nginx,现在需要添加一个未被编译安装的模块举例说明:安装第三方的ngx_cache_purge模块(用于清除指定URL的缓存)nginx的模块是需要重新编译nginx,而不是像apa ...
- 原已经安装好的nginx,现在需要添加一个未被编译安装的模块--echo-nginx-module-0.56
为了测试一个NGINX变量,将NGINX加了一个编译模板echo-nginx-module-0.56. 参照如下文件 1,先看以前NGINX有哪些东东. sbin/nginx -Vnginx vers ...
- Win10系统下安装Oracle服务器和Oracle客户端
工作电脑从Win7换为Win10,在给Win10系统安装Oracle时花费了很长世间终于搞定,在此给大家分享下. 1.工作中需要连接测试环境.生产环境Oracle,所以安装了公司封装的Oracle客户 ...
随机推荐
- 使用java中replaceAll方法替换字符串中的反斜杠
今天在项目中使用java中replaceAll方法将字符串中的反斜杠("\")替换成空字符串(""),结果出现如下的异常: java.util.regex.Pa ...
- Visual Studio 2012使用NUnit单元测试实践01,安装NUnit并使用
在Visual Studio 2012中,默认使用Microsoft自带的MS-Test测试框架.但,Visual Studio同样允许使用第三方测试框架,比如NUnit,xUnit,MbUnit,等 ...
- 在ASP.NET MVC实现购物车,尝试一种不同于平常的购物车显示方式
通常,我们看到的购物车是这样的: 虽然这种购物车显示方式被广泛运用,但我个人觉得不够直观.如果换成这样呢? 本篇的源码放在了:https://github.com/darrenji/ShoppingC ...
- [转发]将Delphi的对象方法设为回调函数
心血来潮,为了实现更好的通用性和封装性,需要把类方法作为回调函数,搜得一篇好文,节选转发.命名似乎应该是MethodToCallback才合适,可惜调试时总是报错,debugging. 原文地址:ht ...
- smartsvn学习(二)如何在Xcode下使用SVN
1.Xcode4中苹果有自带的SVN软件------>Organizer------>Repositories 2.SVN checkout到本地后,删除本地file,对服务器有影响吗 ...
- Ora-01536:超出了表空间users的空间限量
正在开会,同事跑过来说数据库有问题,通讯程序不能入库,赶快获取一条insert into a values()语句后在toad工具中手动插入,发现报错:Ora-01536:超出了表空间users的 ...
- 用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)
用PopupWindow实现弹出菜单是一个比较好的方式.当然我们还有一个类PopupMenu也能实现弹出菜单,但那个太过于局限了,所以不是很推荐. 这个实例的效果是这样的:点击按钮后,一个菜 ...
- Linux下创建可执行bin安装文件
需求及应用场景 1.简化操作.一般的软件安装过程,如果想要精简步骤,我们一般会将需要在命令行中输入的命令写成一个脚本,同时将安装介质准备好.我们将脚本和安装介质上传到生产环境,然后通过执行脚本来完成安 ...
- js混淆代码还原-js反混淆:利用js进行赋值实现
js混淆代码还原-js反混淆:利用js进行赋值实现 [不想用工具的直接看方法二] 本文地址:http://www.cnblogs.com/vnii/archive/2011/12/14/22875 ...
- 【.Net】 C#访问修饰符
一 类的修饰符: C#中类的默认修饰符是internal.1 private 只有对包.NET中的应用程序或库才能访问.2 public 不限制对类的访问. 3 protected 只可以被本类和其 ...