Created
October 10, 2014 17:02
-
-
Save chenzx/1a78b009fe9824355e25 to your computer and use it in GitHub Desktop.
Rust语言的一些特性(基于表达式的系统编程语言?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1、let mut x = 10i; 用学术的话来说,let代表文法级别的名字绑定(到value对象) | |
println!是宏,第一眼看到它时还以为是学习Ruby,!代表函数有副作用呢 | |
destructuring let:解构绑定这个概念来自于LISP/Erlang? | |
2、表达式语言:if是表达式;加上分号就变成语句;return x;等同于直接的一个x | |
还有,它的if/for/while的条件判断不用加()圆括号,这跟Swift是一致的(就是感觉有点别扭) | |
3、函数原型:fn f(x: int, y: &str) -> () { ... } | |
这里使用的语法风格跟C++ 11、Scala似乎都差不多。 | |
4、String是utf-8编码的Unicode字符序列(k,居然强制编码了) | |
string.as_slice() == "Hello" 对比 string == "Hello".to_string() 这里的概念有点怪异,需要好好想一想(语言设计者到底想表达什么意思?) | |
5、Tuple,Struct,Tuple Struct——这3者的区分倒是蛮有意思的,Tuple这个概念应该是来自于Python?对应C++ STL里的pair模板 | |
6、Vector(数组) | |
let nums = [1i, ..20]; // Shorthand for an array of 20 elements all initialized to 1 | |
7、标准输入: | |
let input = std::io::stdin().read_line().ok().expect("Failed to read line"); //IoResult<T> ? | |
8、Cargo包管理器的配置文件后缀名 .toml 取得不大好,感觉有点怪异 | |
9、rand::random::<int>(); 见鬼,这里函数模板实例化的语法也很怪异 | |
作者到底有没有在用心设计语言??? | |
10、crates(独立编译单位?)与模块 | |
mod hello { | |
pub fn print_hello() { | |
println!("Hello, world!"); | |
} | |
} | |
靠 | |
11、集成的单元测试支持? | |
#[test] | |
这里的属性标注为什么一定要加一个#呢?防止语法解析误解析为Vector?但是#已经在许多脚本语言中用作行注释了,这个设计不大好 | |
12、指针 | |
(1)引用:let y = &x; 可以直接引用字面量常量 | |
let mut x = 5i; | |
let y = &mut x; | |
(2)拥有关系、借用与生命周期 | |
(3)装箱(把原语封装为堆对象?) | |
(4)Rc与Arc:(自动)引用计数,不能有循环引用! | |
13、模式匹配 | |
let mut x = 5i; | |
match x { | |
ref mut x => println!("Got a mutable reference to {}", x), | |
} | |
14、闭包 | |
let add_one = |x| { 1i + x }; | |
15、proc(这个概念应该是来自于Ruby,不过现在coroutine这么流行,proc没准过时了?) | |
16、迭代器 | |
for num in nums.iter() { ... } | |
非得加个.iter()吗? | |
17、消费者(略) | |
18、再次学习Scala/C#/Haskell: | |
let x: Option<int> = Some(5i); | |
19、Traits(暂略) | |
20、任务Tasks:这个地方倒是跟Python/Go等有的一拼 | |
21、Unsafe(~C#):用于跟外部数值计算模块连接? | |
来源:http://doc.rust-lang.org/guide.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment