文件名称 | 版本号 | 作者 | 版本 | |
---|---|---|---|---|
Rust日常笔记_持续更新 | v1.0.1 | 学生宫布 | 8416837 | rust 1.44 |
使用Chocolatey
软件包工具安装Rust,Chocolatey
的安装教程见Chocolatey教程。或者使用其它方式安装,下载Rustup。
如果是巧克力方式安装:
执行:choco search rust
rust包被发现,但是我们不需要直接安装rust,先安装rustup
set RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
set RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
以上镜像配置当前页面有效。有可能设置镜像后报ssl
错误。
如果不起效,使用PowerShell设置:
$ENV:RUSTUP_DIST_SERVER='https://mirrors.ustc.edu.cn/rust-static'
$ENV:RUSTUP_UPDATE_ROOT='https://mirrors.ustc.edu.cn/rust-static/rustup'
choco install rustup --pre
输入yes
,开始安装
Installing rustup-init...
晚上安装时,在这个初始化卡住了
rustup toolchain list
rustup default stable-x86_64-pc-windows-msvc
rustup component add rust-src
rustup toolchain install nightly-2019-01-17
orrustup toolchain add nightly
rustup toolchain remove stable
rustup default nightly
link.exe
。请安装VCBuild tools 2015rustc -V
:进入在线编辑
[dependencies]
hyper = "0.12.35"
tokio-core = "0.1.17"
futures = "0.1.29"
如果不引入依赖,则会报错找不到类。
向main.rs
拷入代码:
fn main() {
const X: i32 = 9;
println!("{}", "Hello, world!".to_owned() + &X.to_string());
}
执行结果:
Hello, world!9
有什么感想?比如说,类型是不是比较严格?——Java、Js用+号就可以连接两个对象了
执行rustup
命令:rustup doc --book
,打开web页面,可获得匹配当前rust版本的文档,保证例子可以跑起来。
fn main() {
// let x = 5;
let x = "9";
println!("{}", "Hello, world!".to_owned() + x);
}
to_owned
?{}
进行格式化?use std::thread;
use std::time::Duration;
fn main() {
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});
for i in 1..5 {
println!("hi main number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
}
[dependencies]
hyper = "0.12.35"
tokio-core = "0.1.17"
futures = "0.1.29"
代码:
extern crate futures;
extern crate hyper;
extern crate tokio_core;
use futures::Future;
use hyper::{Client, Uri};
use tokio_core::reactor::Core;
fn main() {
// Core is the Tokio event loop used for making a non-blocking request
let mut core = Core::new().unwrap();
let client = Client::new();
let url: Uri = "http://kunpeng.csdn.net/ad/474".parse().unwrap();
assert_eq!(url.query(), None);
let _request_result = core.run(client
.get(url)
.map(|res| {
println!("Response: {}", res.status());
println!("Response: {:?}", res.body());
})
.map_err(|err| {
println!("Error: {}", err);
})
);
}
.cargo
目录,新建文件名称config
,填入下述内容:[source.crates-io]
replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
[dependencies]
hyper = "0.12.35"
tokio-core = "0.1.17"
futures = "0.1.29"
代码:
fn main() {
// / Create a path to the desired file
let path = Path::new("C:\\dev\\test\\project\\backend\\rust\\rustTestFourth\\src\\One.txt");
let display = path.display();
// Open the path in read-only mode, returns `io::Result`
let mut file = match File::open(&path) {
// The `description` method of `io::Error` returns a string that
// describes the error
Err(why) => panic!("couldn't open {}: {}", display,
why.description()),
Ok(file) => file,
};
// Read the file contents into a string, returns `io::Result`
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => panic!("couldn't read {}: {}", display,
why.description()),
Ok(_) => print!("{} contains:\\n{}", display, s),
}
// `file` goes out of scope, and the "hello.txt" file gets closed
}
响应:文件内容。