public:lang:rust

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
public:lang:rust [2026/05/07 15:12] – 移除 - 外部编辑 (未知日期) 127.0.0.1public:lang:rust [2026/05/07 15:12] (当前版本) – ↷ 页面public:it:rust被移动至public:lang:rust oakfire
行 1: 行 1:
 +====== RUST ======
 +  * 官网:[[http://www.rust-lang.org/|rust-lang.org]]
 +===== tools =====
 +  * [[https://github.com/PistonDevelopers/piston|piston game engine]]
 +  * [[https://github.com/Geal/nom|nom]]
 +  * [[https://github.com/carllerche/mio|mio]]
 +===== Manual =====
 +  * Book:[[https://doc.rust-lang.org/stable/book/|The Rust Programming Language]]; [[https://github.com/KaiserY/trpl-zh-cn|中文版]]
 +  * [[http://rustbyexample.com]]
 +  * 编译型语言, ''rustc''类似gcc/clang, 具体''man rustc'' 或 ''rustc -h''
 +  * Cargo: 官方自带类似 nodejs 的 npm 这样的项目管理工具. 
 +===== 学新语言步骤笔记 =====
 +  * 了解该语言的出生背景与适用环境: 
 +      * 这篇博客介绍很全面:[[http://blog.csdn.net/liigo/article/details/45757123]]
 +      * Rust是一个由Mozilla主导开发的实验性跨平台编译型编程语言。它的设计准则为“安全,并发,实用”,支持函数式,并发式,过程式以及面向对象的编程风格。适用于系统编程.
 +  * 了解输入输入出流,文件流操作:
 +      * 标准输出: 使用宏 ''println!("世界,你好");'', 或 ''std::io''
 +      * 标准输入: ''std::io::stdin().read_line()''
 +      * 文件: ''std::fs''
 +  * 了解程序代码和可执行代码的组织机制,运行时模块加载、符号查找机制:
 +      * 机制:[[http://doc.rust-lang.org/reference.html#crates-and-source-files|crate]],[[http://doc.rust-lang.org/reference.html#items-and-attributes|items,attributes]]
 +      * A **crate** contains a tree of nested module scopes...
 +      * 一个源文件对应一个或多个modules.文件本身就是一个module.
 +      * 代码包含:A module without a body is loaded from an external file, by default with the same name as the module, plus the .rs extension.<code>
 +mod vec;// Load the `vec` module from `vec.rs`
 +mod thread {
 +    // Load the `local_data` module from `thread/local_data.rs`
 +    // or `thread/local_data/mod.rs`.
 +    mod local_data;
 +}
 +</code>
 +      *  items 默认都是私有(除了一点:pub enum里的项默认也是pub),公开需要显式声明''pub''.[[http://doc.rust-lang.org/reference.html#visibility-and-privacy|visibility and privacy]]
 +      * 标准库依赖: rust 默认加载std标准库, 详见文档.
 +      * 外部库: 编译为''.rlib''格式, 用 ''extern crate'' 导入. rustc编译时库路径指定与gcc类似
 +      * ''use ''可以绑定模块接口名到一个新的名字,方便使用,比如''use std::sync::Mutex'',之后就可以直接用''Mutex''这个短名字.
 +      * ''use'',''mod''都受作用域影响.
 +      * 可使用官方提供的 Cargo 工具来方便工程依赖.
 +  * 了解该语言的基本数据类型,基本语法和主要语言构造,主要数学运算符和输入输出函数的使用
 +      * 变量绑定(variable bindings): ''let a = b; let mut somevalue = String::new();'' 默认为常量(they’re [[https://doc.rust-lang.org/stable/book/mutability.html|immutable]] by default), 变量需标明''mut''
 +      * 变量绑定支持模板(patten)匹配, 类似erlang;
 +      * 基本数据类型:  [[https://doc.rust-lang.org/stable/book/primitive-types.html|primitive-types]]
 +      * 条件: ''match statement'', ''if a==b { } else { }''. ''if''也是表达式, 可被变量绑定''let a= if b==c {b} else {c};''
 +      * 循环: ''loop{}'', ''for x in y{}'', ''for( ; ;){}'', ''while'', ''continue'', ''break''.
 +      * [[https://doc.rust-lang.org/stable/book/functions.html#expressions-vs.-statements|expression-based language]], 主表达式语言, 表达式始终返回一个值, rust 函数也属于表达式; 但是 let 语句不是表达式(属于声明);
 +      * enum 比较强大,感觉更类似tuple.
 +      * **char** 类型为32位无符号Unicode,UCS-4/UTF-32编码
 +      * **str** 类型为8位无符号array, UTF-8编码
 +      * 支持 **type** 自定义类型
 +  * 了解数组和其他集合类的使用:
 +      * Array: [[https://doc.rust-lang.org/stable/std/vec/|Vec<T>]]; ''vec!'' macro.
 +      * Slice FIXME 
 +      * Tuple FIXME 
 +  * 了解字符串的处理
 +      * 不同于老的语言,rust 基础字符串相关类型**char**,**str**默认就支持unicode.
 +      * String(默认utf8编码); 与str的转换 ''to_string()''
 +      * 由于是utf8编码,所以str支持两种下标:按字节与按文字:<code rust>
 +let hachiko = "忠犬ハチ公";
 +for b in hachiko.as_bytes() {
 +    print!("{}, ", b);
 +}
 +println!("");
 +for c in hachiko.chars() {
 +    print!("{}, ", c);
 +}
 +println!("");
 +
 +// This prints:
 +// 229, 191, 160, 231, 138, 172, 227, 131, 143, 227, 131, 129, 229, 133, 172, 
 +// 忠, 犬, ハ, チ, 公, 
 +</code>
 +      * 由于是utf8编码,所以rust的str不需要null结尾,字符串本身也可包含null.
 +  * 了解该语言在面向对象,函数式编程,泛型,元编程等编程范式的特性
 +      * 并发:[[https://doc.rust-lang.org/stable/book/concurrency.html|concurrency]]
 +      * OO: ''struct'', ''impl'', 没有方法重载,使用[[https://doc.rust-lang.org/stable/book/method-syntax.html#builder-pattern|builder-pattern]]来替代重载.
 +      * OO: ''Drop''接口, 类似c++析构函数
 +      * 暂时没有继承
 +      * 支持匿名函数,支持闭包;
 +      * 函数作为一种类型, 仍需指定参数与返回值, 类似C++函数指针:''let mut myfun:fn(i32,i32)->i32'';
 +      * 支持泛型(Generics):包括 函数与struct. 语法类似c++
 +      * 支持接口 ''trait ''编程, 扩展了OO与泛型: 参数可声明为''fn myfun<T:SomeMethod> (a:T){}'' 其中''T:SomeMethod''表示拥有接口''SomeMethod''的任意T.
 +  * 了解特有的语法糖
 +      * [[http://doc.rust-lang.org/reference.html#if-let-expressions|if let]] 
 +  * 了解该语言错误处理,调试方式以及对测试的支持
 +      * 没有 exception. [[https://doc.rust-lang.org/stable/book/error-handling.html|error hadling]]
 +      * 方法调用可返回 [[https://doc.rust-lang.org/stable/std/result/enum.Result.html|Result]], 如果未对 Result 进行处理, 编译器会警告;
 +      * Result 可有''ok()''等方法来方便进行错误处理. 
 +      * 调试方式: gdb
 +      * 测试支持: cargo test
 +  * 了解该语言的内存分配机制或GC,线程,进程等运行时效率相关
 +      * 8-)不需要手动释放内存, 没有运行时GC,但可在编译期来保证内存安全!
 +      * [[https://doc.rust-lang.org/stable/book/ownership.html|Ownership]]: 所有权唯一;
 +      * 引用(reference, borrow),可以有多个不变引用,或一个可变引用,但不能有两种引用同时存在;引用生存期不能比所有者长;
 +      * 生存期(lifetimes), 可显式声明 '' 'a '', '' 'static '';
 +  * 了解该语言的编译/解释机制
 +
 +===== Others =====
 +  * 注释:<code>
 +// 两斜线为普通注释;  
 +
 +/// 三斜线支持markdown格式, 
 +/// 三个斜线的注释可被 cargo doc 生成文档
 +</code>
 +
 +
 +
 +