$ brew update
...
$ brew install rust
==> Downloading https://homebrew.bintray.com/bottles/rust-1.0.0.yosemite.bottle.tar.gz
############################################################ 100.0%
==> Pouring rust-1.0.0.yosemite.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
zsh completion has been installed to:
/usr/local/share/zsh/site-functions
==> Summary
/usr/local/Cellar/rust/1.0.0: 13947 files, 353M
$ cat > hello_world.rs
fn main() {
println!("hello world");
}
^D
$ rustc hello_world.rs
$ ./hello_world
hello world
$
#[macro_use] extern crate nickel;
use nickel::Nickel;
fn main() {
let mut server = Nickel::new();
server.utilize(router! {
get "**" => |_req, _res| {
"Hello world!"
}
});
server.listen("127.0.0.1:6767");
}
curl -LO https://static.rust-lang.org/rustup.sh
$ which cargo
/usr/local/bin/cargo
$ cargo -v
Rust 包管理器
用法:
cargo <命令> [<参数>...]
cargo [选项]
选项:
-h, --help 显示帮助信息
-V, --version 显示版本信息并退出
--list 安装命令列表
-v, --verbose 使用详细的输出
常见的 cargo 命令:
build 编译当前工程
clean 删除目标目录
doc 编译此工程及其依赖项文档
new 创建一个新的 cargo 工程
run 编译并执行 src/main.rs
test 运行测试
bench 运行基准测试
update 更新 Cargo.lock 中的依赖项
search 搜索注册过的 crates
执行 'cargo help <command>' 获取指定命令的更多帮助信息.
$ rm rustup.sh
$ cargo new . --bin
目标 `/Users/joel/Projects/simplelog/.` 已经存在
$ cargo -h
在 <路径> 处创建一个新的 Cargo 包.
用法:
cargo new [选项] <路径>
cargo new -h | --help
选项:
-h, --help 显示帮助信息
--vcs <vcs> 为指定的版本管理系统(git 或 hg)
初始化一个新仓库
或者不使用版本管理系统(none)
--bin 创建可执行文件工程而不是库工程
--name <name> 设置结果包名
-v, --verbose 使用详细的输出
$ cd ../
$ rm -rf simplelog/
$ cargo new simple-log --bin
$ cd simple-log/
$ tree
.
|____.git
| |____config
| |____description
| |____HEAD
| |____hooks
| | |____README.sample
| |____info
| | |____exclude
| |____objects
| | |____info
| | |____pack
| |____refs
| | |____heads
| | |____tags
|____.gitignore
|____Cargo.toml
|____src
| |____main.rs
[package]
name = "simple-log"
version = "0.1.0"
authors = ["Joel McCracken <mccracken.joel@gmail.com>"]
[dependencies.nickel]
git = "https://github.com/nickel-org/nickel.rs.git"
#[macro_use] extern crate nickel;
use nickel::Nickel;
fn main() {
let mut server = Nickel::new();
server.utilize(router! {
get "**" => |_req, _res| {
"Hello world!"
}
});
server.listen("127.0.0.1:6767");
}
cargo run
Updating git repository `https://github.com/nickel-org/nickel.rs.git`
Updating registry `https://github.com/rust-lang/crates.io-index`
Downloading unsafe-any v0.4.1
Downloading httparse v0.1.2
Downloading traitobject v0.0.3
Downloading lazy_static v0.1.10
Downloading pkg-config v0.3.4
Downloading num_cpus v0.2.5
Downloading modifier v0.1.0
Downloading groupable v0.2.0
Downloading unicase v0.1.0
Downloading gcc v0.3.5
Downloading time v0.1.25
Downloading log v0.3.1
Downloading url v0.2.34
Downloading plugin v0.2.6
Downloading hyper v0.4.0
Downloading matches v0.1.2
Downloading mustache v0.6.1
Downloading bitflags v0.1.1
Downloading typeable v0.1.1
Downloading openssl v0.6.2
Downloading rustc-serialize v0.3.14
Downloading typemap v0.3.2
Downloading regex v0.1.30
Downloading cookie v0.1.20
Downloading mime v0.0.11
Downloading libc v0.1.8
Downloading openssl-sys v0.6.2
Compiling modifier v0.1.0
Compiling traitobject v0.0.3
Compiling regex v0.1.30
Compiling libc v0.1.8
Compiling lazy_static v0.1.10
Compiling matches v0.1.2
Compiling httparse v0.1.2
Compiling rustc-serialize v0.3.14
Compiling groupable v0.2.0
Compiling pkg-config v0.3.4
Compiling gcc v0.3.5
Compiling bitflags v0.1.1
Compiling unicase v0.1.0
Compiling typeable v0.1.1
Compiling unsafe-any v0.4.1
Compiling log v0.3.1
Compiling num_cpus v0.2.5
Compiling typemap v0.3.2
Compiling mime v0.0.11
Compiling plugin v0.2.6
Compiling openssl-sys v0.6.2
Compiling time v0.1.25
Compiling openssl v0.6.2
Compiling url v0.2.34
Compiling mustache v0.6.1
Compiling cookie v0.1.20
Compiling hyper v0.4.0
Compiling nickel v0.5.0 (https://github.com/nickel-org/nickel.rs.git#69546f58)
Compiling simple-log v0.1.0 (file:///Users/joel/Projects/simple-log)
Running `target/debug/simple-log`
Listening on http://127.0.0.1:6767
Ctrl-C to shutdown server
^C
fn say_hello() {
"Hello dear world!";
}
fn main() {
let mut server = Nickel::new();
server.utilize(router! {
get "**" => |_req, _res| {
say_hello();
}
});
server.listen("127.0.0.1:6767");
}
fn say_hello() {
"Hello dear world!"
}
fn main() {
let mut server = Nickel::new();
server.utilize(router! {
get "**" => |_req, _res| {
say_hello()
}
});
server.listen("127.0.0.1:6767");
}
$ cargo run
Compiling simple-log v0.1.0 (file:///Users/joel/Projects/simple-log)
src/main.rs:6:5: 6:24 错误:不匹配的类型:
预期 `()`,
找到 `&'static str`
(预期 (),
找到 &-ptr) [E0308]
src/main.rs:6 "Hello dear world!"
^~~~~~~~~~~~~~~~~~~
错误:由于先前的错误而中止
不能编译 `simple-log`.
想查看更多信息,请加上 --verbose 重新运行命令.
#[macro_use] extern crate nickel;
use nickel::Nickel;
fn say_hello() -> &'static str {
"Hello dear world!"
}
fn main() {
let mut server = Nickel::new();
server.utilize(router! {
get "**" => |_req, _res| {
say_hello()
}
});
server.listen("127.0.0.1:6767");
}
$ cargo run &
[1] 14997
Running `target/debug/simple-log`
Listening on http://127.0.0.1:6767
Ctrl-C to shutdown server
$ curl http://localhost:6767
Hello dear world!
$ fg
cargo run
^C
来源: https://juejin.im/post/5a69325b6fb9a01ca072043d