主要参考资料:Rust 程序设计语言
github地址:https://github.com/yunwei37/os-summer-of-code-daily
环境:
uname -a
Linux ubuntu 5.4.0-39-generic #43-Ubuntu SMP Fri Jun 19 10:28:31 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
运行:
curl https://sh.rustup.rs -sSf | sh
Welcome to Rust!
Rust is installed now. Great!
重启;
rustc --version
rustc 1.44.1 (c7087fe00 2020-06-17)
很新鲜的样子
查看文档:rustup doc
装了个插件:Rust support for Visual Studio Code
然后跳了个框,说有些组件还没装…
$ mkdir hello_world
$ cd hello_world
$ cd hello_world
输入:
fn main() {
println!("Hello, world!");
}
yunwei@ubuntu:~/hello_world$ rustc main.rs
yunwei@ubuntu:~/hello_world$ ./main
Hello, world!
Cargo 是 Rust 的构建系统和包管理器。
yunwei@ubuntu:~/hello_world$ cargo --version
cargo 1.44.1 (88ba85757 2020-06-11)
yunwei@ubuntu:~$ cargo new hello_cargo
Created binary (application) `hello_cargo` package
yunwei@ubuntu:~$ cd hello_cargo
文件名: Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["yunwei <[email protected]>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
yunwei@ubuntu:~/hello_cargo$ cargo build
Compiling hello_cargo v0.1.0 (/home/yunwei/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.72s
yunwei@ubuntu:~/hello_cargo$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/hello_cargo`
Hello, world!
yunwei@ubuntu:~/hello_cargo$ cargo check
Checking hello_cargo v0.1.0 (/home/yunwei/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.11s
Some common cargo commands are (see all commands with --list):
build Compile the current package
check Analyze the current package and report errors, but don't build object files
clean Remove the target directory
doc Build this package's and its dependencies' documentation
new Create a new cargo package
init Create a new cargo package in an existing directory
run Run a binary or example of the local package
test Run the tests
bench Run the benchmarks
update Update dependencies listed in Cargo.lock
search Search registry for crates
publish Package and upload this package to the registry
install Install a Rust binary. Default location is $HOME/.cargo/bin
uninstall Uninstall a Rust binary
关联函数
静态方法
引用
(reference),它允许多处代码访问同一处数据,而无需在内存中多次拷贝。枚举
(enumerations),通常也写作 enums。枚举类型持有固定集合的值,这些值被称为枚举的 成员(variants)。Result
的成员是 Ok
和 Err
碰到了这个
yunwei@ubuntu:~/guessing_game$ cargo build
Blocking waiting for file lock on package cache
解决方法:删除~/.cargo/.package_cache文件,然后 cargo clean ; cargo build
源代码:
use rand::Rng;
use std::io;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1, 101);
println!("The secret number is: {}", secret_number);
loop {
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = guess.trim().parse().expect("Please type a number!");
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}