Upload
-
View
3.085
Download
12
Embed Size (px)
DESCRIPTION
珠三角技术沙龙广州场2012年2月新语言专场
Citation preview
为什么 rust-lang 吸引我?
赖勇浩2012.2.25
rust-lang
• 实用• 并发• 安全
简介• www.rust-lang.org• github.com/mozilla/ru
st• @rustlang• MIT License• v0.1 2012.1.20• Linux, OSX, Windows
背景• mozilla servo prj
• 2006~
• 2010~2011 自举• Graydon Hoare
– tracemonkey JIT backend, nanoJIT
– XPCOM cycle collector
hello world.
use std;
fn main(args: [str]) {
std::io::println("hello world.");
}
compile&run
$:rustc hello.rs
$:./hello
hello world.
compile error
hello.rs:2:4: 2:20 error: unresolved modulename: std
hello.rs:2 std::io::println("hello world!");
^~~~~~~~~~~~~~~~
实用
包管理与网络编程• cargo
• libuv
• 更容易调用 C code
丰富的类型• ()• bool• int/uint• i8, i16, i32, i64• u8, u16, u32, u64• float• f32, f64• char• str
• [T]• [mutable T]• (T1, T2)• {field1: T1, field2: T2}• fn(arg1: T1, arg2: T2)
-> T3, fn@(), fn~(), fn&()
• @T, ~T, *T
Expression syntax
• everything that's not a declaration is an expression.
let x = if the_stars_align() { 4 }
else if something_else() { 3 }
else { 0 };
fn is_four(x: int) -> bool { x == 4 }
Attributes
#[cfg(target_os = "win32")]
fn register_win_service() { /* ... */ }
#[link(name = "std",
vers = "0.1",
url = "http://rust-lang.org/src/std")];
Syntax extensions
std::io::println(#fmt("%s is %d", "the answer", 42));
std::io::println(#env("PATH"));
log(warn, "hi");log(error, (1, [2.5, -1.8]));#warn("only %d seconds remaining", 10);#error("fatal: %s", get_error_string());
Pattern matching
alt my_number {
0 { std::io::println("zero"); }
1 | 2 { std::io::println("one or two"); }
3 to 10 { std::io::println("three to ten"); }
_ { std::io::println("something else"); }
}
Pattern matching
fn angle(vec: (float, float)) -> float {
alt vec {
(0f, y) if y < 0f { 1.5 * float::consts::pi }
(0f, y) { 0.5 * float::consts::pi }
(x, y) { float::atan(y / x) }
}
}
Loops
let x = 5;
while true {
x += x - 3;
if x % 5 == 0 { break; }
std::io::println(int::str(x));
}
do { eat_cake();} while any_cake_left();
for elt in ["red", "green", "blue"] {
std::io::println(elt);}
Closures
• fn call_closure_with_ten(b: fn(int)) { b(10); }
• let x = 20;
• call_closure_with_ten({|arg|
• #info("x=%d, arg=%d", x, arg);
• });
Boxed closures
• 将 closures 破储在数据结构中
• 拥有任意的生存期
use std;
fn mk_appender(suffix: str) -> fn@(str) -> str {
let f = fn@(s: str) -> str { s + suffix };
ret f;
}
fn main() {
let shout = mk_appender("!");
std::io::println(shout("hey ho, let's go"));
}
Binding
let daynum = bind vec::position_elt(["mo", "tu", "we", "do", "fr", "sa", "su"], _);
产生一个 boxed closure
Generics
fn for_rev<T>(v: [T], act: fn(T)) {
let i = vec::len(v);
while i > 0u {
i -= 1u;
act(v[i]);
}
}
fn map<T, U>(v: [T], f: fn(T) -> U) -> [U] {
let acc = [];
for elt in v { acc += [f(elt)]; }
ret acc;
}
Generic datatypes
type circular_buf<T> = {start: uint,
end: uint,
buf: [mutable T]};
enum option<T> { some(T), none }
其它• Modules and crates• Interfaces• interoperate with C code
#[cfg(target_os = "win32")]
#[abi = "stdcall"]
native mod kernel32 {
fn SetEnvironmentVariableA(n: *u8, v: *u8) -> int;
}
• Tasks
• Testing#[test]fn test_twice() { let i = -100;...
• > ./twicerunning 1 teststest test_twice ... okresult: ok. 1 passed; 0 f
ailed; 0 ignored
并发
Spawning a task
• let some_value = 22;
• task::spawn {||
• std::io::println("This executes in the child task.");
• std::io::println(#fmt("%d", some_value));
• }
Ports and channels
let port = comm::port::<int>();let chan = comm::chan::<int>(port);let child_task = task::spawn {|| let result =
some_expensive_computation(); comm::send(chan, result);};some_other_expensive_computation();let result = comm::recv(port);
Creating a task with a bi-directional communication path
fn stringifier(from_parent: comm::port<uint>, to_parent: comm::chan<str>) { let value: uint; do { value = comm::recv(from_parent); comm::send(to_parent, uint::to_str(value,
10u)); } while value != 0u;}
Creating a task with a bi-directional communication path
fn main() { let from_child = comm::port(); let to_parent = comm::chan(from_child); let to_child = task::spawn_listener {|
from_parent| stringifier(from_parent, to_parent); }; comm::send(to_child, 22u); assert comm::recv(from_child) == "22";}
• A unique box type or value is constructed by the prefix tilde sigil ~.
• let x: ~int = ~10;
安全
下次,下次再讲。
Ubuntu PPA
• https://launchpad.net/~kevincantu/+archive/rust
• Ubuntu 10.04/11.10
• Thank you, Kevin Cantu.
http://laiyonghao.com