Skip to content

Commit

Permalink
上线极客学院wiki
Browse files Browse the repository at this point in the history
添加 toc、config、cover文件与校对格式
  • Loading branch information
twq0076262 committed Jul 19, 2016
1 parent 3d4426a commit 4f78854
Show file tree
Hide file tree
Showing 115 changed files with 620 additions and 70 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[build-url]: https://travis-ci.org/rustcc/RustPrimer

The Rust primer for beginners.

给初学者的Rust中文教程。

## 在线阅读地址
Expand Down
117 changes: 117 additions & 0 deletions TOC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
- [初识Rust](1st-glance/README.md)
- [安装Rust](install/preface.md)
- [Linux](install/install_rust_on_linux.md)
- [Mac](install/install_rust_on_mac_os.md)
- [Windows](install/install_rust_on_windows.md)
- [版本管理工具: rustup](install/rustup.md)
- [编辑器](editors/preface.md)
- [前期准备](editors/before.md)
- [vim](editors/vim.md)
- [emacs](editors/emacs.md)
- [vscode](editors/vscode.md)
- [atom](editors/atom.md)
- [sublime](editors/sublime.md)
- [visual studio](editors/visualstudio.md)
- [spacemacs](editors/spacemacs.md)
- [Rust快速入门](quickstart/quickstart.md)
- [Rust旅程](quickstart/rust-travel.md)
- [变量绑定与原生类型](quickstart/primitive-type.md)
- [数组、动态数组和字符串](quickstart/vector-string.md)
- [结构体与枚举](quickstart/struct-enum.md)
- [控制流](quickstart/control-flow.md)
- [函数与方法](quickstart/function-method.md)
- [特性](quickstart/trait.md)
- [注释与文档](quickstart/comments-document.md)
- [输入输出流](quickstart/io-stream.md)
- [Cargo项目管理器](cargo-projects-manager/cargo-projects-manager.md)
- [基本程序结构](flow/preface.md)
- [注释](flow/comment.md)
- [条件](flow/condition.md)
- [循环](flow/repetition.md)
- [类型、运算符和字符串](type/preface.md)
- [基础类型](type/types.md)
- [复合类型](type/compound-types.md)
- [字符串类](type/string.md)
- [基础运算符和字符串格式化](type/operator-and-formatting.md)
- [函数](function/overview.md)
- [函数参数](function/arguement.md)
- [函数返回值](function/return_value.md)
- [语句和表达式](function/statement_expression.md)
- [高阶函数](function/higher_order_function.md)
- [模式匹配](match/overview.md)
- [match关键字](match/match.md)
- [模式 pattern](match/pattern.md)
- [特征 Trait](trait/overview.md)
- [trait关键字](trait/trait.md)
- [trait对象](trait/trait-object.md)
- [泛型](generic/generic.md)
- [可变性、所有权、租借和生命期](ownership-system/ownership_system.md)
- [所有权](ownership-system/ownership.md)
- [引用和借用](ownership-system/borrowing_reference.md)
- [生命周期](ownership-system/lifetime.md)
- [闭包](closure/overview.md)
- [闭包的语法](closure/syntax.md)
- [闭包的实现](closure/implementation.md)
- [闭包作为参数和返回值](closure/as_argument_return_value.md)
- [集合类型 Collections](collections/overview.md)
- [动态数组 Vec](collections/vec.md)
- [哈希表 HashMap](collections/hashmap.md)
- [迭代器](iterator/overview.md)
- [迭代器、适配器、消费者](iterator/iterator.md)
- [模块和包系统、Prelude](module/preface.md)
- [模块 module 和包 crate](module/module.md)
- [Prelude](module/prelude.md)
- [Option、Result与错误处理](error-handling/option-result.md)
- [输入与输出](io/io.md)
- [宏系统](macro/macro.md)
- [堆、栈与Box](heap-stack/heap-stack.md)
- [几种智能指针](rcarc/preface.md)
- [Rc, Arc](rcarc/rcarc.md)
- [Mutex, RwLock](rcarc/mutex.md)
- [Cell, RefCell](rcarc/cell.md)
- [类型系统中的几个常见 Trait](intoborrow/preface.md)
- [Into/From 及其在 String 和 &str 互转上的应用](intoborrow/into.md)
- [AsRef, AsMut](intoborrow/asref.md)
- [Borrow, BorrowMut, ToOwned](intoborrow/borrow.md)
- [Deref 与 Deref coercions](intoborrow/deref.md)
- [Cow 及其在 String 和 &str 上的应用](intoborrow/cow.md)
- [Send 和 Sync](marker/sendsync.md)
- [并发,并行,多线程编程](concurrency-parallel-thread/preface.md)
- [线程](concurrency-parallel-thread/thread.md)
- [消息传递](concurrency-parallel-thread/message-passing.md)
- [共享内存](concurrency-parallel-thread/share-memory.md)
- [同步](concurrency-parallel-thread/synchronize.md)
- [并行](concurrency-parallel-thread/parallel.md)
- [Unsafe、原始指针](unsafe-rawpointer/preface.md)
- [Unsafe](unsafe-rawpointer/unsafe.md)
- [原始指针](unsafe-rawpointer/raw-pointer.md)
- [FFI](ffi/preface.md)
- [rust调用ffi函数](ffi/calling-ffi-function.md)
- [将rust编译成库](ffi/compiling-rust-to-lib.md)
- [运算符重载](operator-overloading/operator.md)
- [属性和编译器参数](attr-and-compiler-arg/preface.md)
- [属性](attr-and-compiler-arg/attribute.md)
- [编译器参数](attr-and-compiler-arg/rustc-option.md)
- [Cargo参数配置](cargo-detailed-cfg/cargo-detailed-cfg.md)
- [测试与评测](testing/preface.md)
- [测试 (testing)](testing/threearchtest.md)
- [评测 (benchmark)](testing/bench.md)
- [代码风格](coding-style/style.md)
- [Any与反射](any/any.md)
- [安全(safe)](safe/safety.md)
- [常用数据结构实现](data-structure/preface.md)
- [栈结构](data-structure/stack.md)
- [队列](data-structure/queue.md)
- [二叉树](data-structure/binary_tree.md)
- [优先队列](data-structure/priority_queue.md)
- [链表](data-structure/linked_list.md)
- [图结构](data-structure/graph.md)
- [标准库介绍](std/overview.md)
- [系统命令:调用grep](std/process.md)
- [目录操作:简单grep](std/fs-and-path.md)
- [网络模块:W回音](std/net.md)
- [实战篇](action/preface.md)
- [实战:Json处理](action/json_data/readme.md)
- [实战:Web 应用开发入门](action/mysite/readme.md)
- [实战:使用Postgresql数据库](action/db/readme.md)
- [附录-术语表](appendix/glossary.md)
5 changes: 5 additions & 0 deletions action/db/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub fn query_all(conn: &Connection,query: &str){
}

```

然后在main.rs 中调用相应的函数代码如下
1. extern db ,引入db,也就是将项目本身引入
2. use db 使用db,中的可以被引入的函数
Expand Down Expand Up @@ -182,6 +183,7 @@ fn main() {
```

自己遇到的坑

- 创建连接函数时,连接必须有一个返回值,所以必须指定返回值的类型,
对于一个写python的人而言,我觉得是痛苦的,我想按照官方的写法match
一下,发现可能产生多个返回值。在编译时直接无法通过编译,所以最终
Expand All @@ -202,7 +204,9 @@ pub fn query_all(conn: &Connection,query: &str){
}

```

报错如下:

``` rust
vagrant@ubuntu-14:~/tmp/test/rustprimer/db$ cargo run
Compiling db v0.1.0 (file:///home/vagrant/tmp/test/rustprimer/db)
Expand All @@ -217,6 +221,7 @@ error: aborting due to previous error
Could not compile `db`.

```

然后去查看了关于postgres模块的所有函数,尝试了无数种办法,依旧没有解决。

可能自己眼高手低,如果从头再把rust的相关教程看一下,可能很早就发现这个问题,
Expand Down
15 changes: 15 additions & 0 deletions action/json_data/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ json是一种比较重要的格式,尤其是现在的web开发领域,json相
rust中的json处理依赖 cargo 中的rustc-serialize模块

###先简单的创建一个rust项目工程

``` rust
$ cargo new json_data --bin
```

生成文件树:

```shell
vagrant@ubuntu-14:~/tmp/test/rustprimer$ tree
.
Expand All @@ -19,6 +22,7 @@ vagrant@ubuntu-14:~/tmp/test/rustprimer$ tree
```

生成项目`json_data`,项目下文件介绍:

- Caogo.toml ,文件中填写一些项目的相关信息,比如版本号,联系人,项目名,文件的内容如下:
Expand All @@ -39,6 +43,7 @@ authors = ["wangxxx <[email protected]>"]

rustc-serialize 这个是第三方的模块,需要从[cargo](https://crates.io/crates/rustc-serialize)下载。
下载很简单,只需修改一下cargo.toml文件就行了.

```toml
[package]
name = "json_data"
Expand All @@ -49,12 +54,16 @@ authors = ["wangxxx <[email protected]>"]
rustc-serialize = "0.3.18"
```

然后执行在当前目录执行:

```
$ cargo build
```
*注意一个问题由于国内网络访问github不稳定,这些第三方库很多托管在github上,所以可能需要修改你的
网络访问*
1. 在安装rust之后,会在你的用户目录之下生成一个`.cargo`文件夹,进入这个文件夹
2. 在`.cargo`文件夹下,创建一个`config`文件,在文件中填写中科大软件源,可能以后会出现其他的源,先用这个
3. `config`文件内容如下
Expand All @@ -64,6 +73,7 @@ $ cargo build
index = "git://crates.mirrors.ustc.edu.cn/index"
```

cargo build 执行之后的提示信息

```
Expand All @@ -72,6 +82,7 @@ cargo build 执行之后的提示信息
Compiling rustc-serialize v0.3.18 (registry git://crates.mirrors.ustc.edu.cn/index)
Compiling json_data v0.1.0 (file:///home/vagrant/tmp/test/rustprimer/json_data)
```

再次执行tree命令:

```
Expand All @@ -90,6 +101,7 @@ cargo build 执行之后的提示信息
`-- native
```

可以看到多了很多文件,重点关注`cargo.lock`,开打文件:

```toml
Expand All @@ -106,9 +118,11 @@ version = "0.3.18"
source = "registry+git://crates.mirrors.ustc.edu.cn/index"

```

是关于项目编译的一些依赖信息

还有生成了target文件夹,生成了可执行文件json_data,因为main.rs中的执行结果就是打印`hello world`

```
$ cargo run
Expand Down Expand Up @@ -152,4 +166,5 @@ fn main() {
}

```

当然我们也可以在文本中作为api的返回结果使用,下来的章节中,我们将讨论这个问题
16 changes: 16 additions & 0 deletions action/mysite/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ rust目前比较有名的框架是iron和nickel,我们两个都写一下简单
接上一篇,使用cargo获取第三方库。`cargo new mysite --bin`

在cargo.toml中添加iron的依赖,

```toml
[dependencies]
iron = "*"
```

然后build将依赖下载到本地 `cargo build`

如果报ssl错误,那可能你需要安装linux的ssl开发库。
Expand All @@ -32,6 +34,7 @@ fn main() {
}).http("localhost:3000").unwrap();
}
```

然后运行

`cargo run`
Expand All @@ -53,6 +56,7 @@ let plus_one = |x: i32| x + 1;

assert_eq!(2, plus_one(1));
```

具体的怎么使用 ,可以暂时不用理会,因为你只要知道如何完成web,因为我也不会。。
结合之前一章节的json处理,我们来看看web接口怎么返回json,当然也要 rustc_serialize 放到 cargo.toml 中

Expand Down Expand Up @@ -82,12 +86,14 @@ fn main() {
println!("On 3000");
}
```

执行 cargo run 使用 curl 测试结果:

```
curl localhost:3000
{"msg":"Hello, World"}
```

当然可以可以实现更多的业务需求,通过控制自己的json。

既然有了json了,如果要多个路由什么的,岂不是完蛋了,所以不可能这样的,我们需要考虑一下怎么实现路由的定制
Expand Down Expand Up @@ -133,8 +139,10 @@ fn main() {
Iron::new(router).http("localhost:3000").unwrap();
}
```

这次添加了路由的实现和获取客户端发送过来的数据,有了get,post,所以现在一个基本的api网站已经完成了。不过
并不是所有的网站都是api来访问,同样需要html模版引擎和直接返回静态页面。等等

```
vagrant@ubuntu-14:~/tmp/test/rustprimer/mysite$ cargo build
Compiling mysite v0.1.0 (file:///home/vagrant/tmp/test/rustprimer/mysite)
Expand All @@ -146,6 +154,7 @@ src/main.rs:29:36: 29:52 help: candidate #1: use `std::io::Read`
error: aborting due to previous error
Could not compile `mysite`.
```

编译出错了,太糟糕了,提示说没有read_to_string这个方法,然后我去文档查了一下,发现有[read_to_string方法](http://ironframework.io/doc/iron/request/struct.Body.html)
再看提示信息

Expand Down Expand Up @@ -201,6 +210,7 @@ let mut buffer = String::new();
try!(f.read_to_string(&mut buffer));

```

用法比较简单,我们修改一下刚刚的函数:

```
Expand All @@ -213,12 +223,14 @@ fn set_greeting(request: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Ok, payload)))
}
```

从request中读取字符串,读取的结果存放到payload中,然后就可以进行操作了,编译之后运行,使用curl提交一个post数据

```
$curl -X POST -d '{"msg":"Just trust the Rust"}' http://localhost:3000/set
{"msg":"Just trust the Rust"}
```

iron 基本告一段落
当然还有如何使用html模版引擎,那就是直接看文档就行了。

Expand Down Expand Up @@ -247,6 +259,7 @@ fn main() {
server.listen("127.0.0.1:6767");
}
```

简单来看,也就是这样回事。

1. 引入了nickel的宏
Expand Down Expand Up @@ -276,18 +289,21 @@ fn main() {
}

```

上面的信息你可以编译,使用curl看看发现出现

```
$ curl http://127.0.0.1:6767
Internal Server Error
```

看看文档,没发现什么问题,我紧紧更换了一个文件夹的名字,这个文件夹我也创建了。
然后我在想难道是服务器将目录写死了吗?于是将上面的路径改正这个,问题解决。

```rust
return response.render("examples/assets/template.tpl", &data);
```

我们看一下目录结构

```
Expand Down
2 changes: 1 addition & 1 deletion attr-and-compiler-arg/rustc-option.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 编译器参数
# 编译器参数

本章将介绍Rust编译器的参数。

Expand Down
Loading

0 comments on commit 4f78854

Please sign in to comment.