Rust 初体验
2016-03-09 22:34
357 查看
一、环境
二、安装
$curl -sSf https://static.rust-lang.org/rustup.sh | sh
三、查看
$rustc --version
$cargo --version
四、创建项目目录结构
$cd ~$mkdir develop$cd develop/$mkdir rust-projects$cd rust-projects/$mkdir hello_world$cd hello_world/
五、rust 之 helloworld$vi main.rs
$rustc main.rs$./main
六、cargo 之 helloworld当前目录在 hello_world1、基本的cargo 命令行$mkdir src$mv main.rs src/main.rs$rm main $vi Cargo.tomlCargo.toml的内容如下
$cargo build
$./target/debug/hello_world
$cargo run
$cargo build --release
2、cargo项目的管理工具1) 自动生成项目$cd ..$mkdir cargo_test$cd cargo_test/
$cargo new hello_world --bin$cd hello_world/$cat Cargo.toml //查看cargo自动生成的配置文件
$cd src/$cat main.rs //查看cargo自动生成的helloworld代码
2)猜数游戏$cd ..$cargo new guessing_game --bin$cd guessing_game/$cargo build
$cargo run
$vi src/main.rs
$cargo run
$vi Cargo.toml //增加 dependencies
$cargo build
如果出现 unable to get packages from source则需要多次反复执行 cargo build 或者 cargo build -verbose,直到
修改代码,生成随机数
$vi src/main.rs
$cargo build
$cargo run
原文地址: https://doc.rust-lang.org/book/getting-started.html https://doc.rust-lang.org/book/guessing-game.html
二、安装
$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_world1、基本的cargo 命令行$mkdir src$mv main.rs src/main.rs$rm main $vi Cargo.tomlCargo.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
相关文章推荐
- SQL Tune Report–sqltrpt.sql
- python编码,三个编码实例
- iOS实现(超级猜图)源码
- ES6新特性概览
- 我的SLAM探索(二)--ROS下编译运行Kinect Viewer
- Eclipse launch configuration 一
- IE下判断IE版本的代码语句<!--[if gte IE 6]> Only IE 6和以上版本 <![endif]-->
- Verilog笔记
- 程序员面试金典(排序与查找):元素查找(java解法)
- Grandle编译时报错:Could not create plugin of type 'AppPlugin'
- 开发工具_新版CocoaPods安装及注意事项
- 2016-03-09
- Android异步消息处理机制(一):简单使用
- C++模板总结
- Xcode安装插件误点[Skip Bundle]后需重新[Load Bundle]的解决办法
- 用循环求
- Android Transition框架介绍(1)
- SpringMVC将数据放到作用域中的方式
- 深入理解python的yield和generator
- 矩阵快速幂