32
为为为 rust-lang 为为为为为为 2012.2.25

为什么 rust-lang 吸引我?

  • Upload
    -

  • View
    3.079

  • Download
    12

Embed Size (px)

DESCRIPTION

珠三角技术沙龙广州场2012年2月新语言专场

Citation preview

Page 1: 为什么 rust-lang 吸引我?

为什么 rust-lang 吸引我?

赖勇浩2012.2.25

Page 2: 为什么 rust-lang 吸引我?

rust-lang

• 实用• 并发• 安全

Page 3: 为什么 rust-lang 吸引我?

简介• www.rust-lang.org• github.com/mozilla/ru

st• @rustlang• MIT License• v0.1 2012.1.20• Linux, OSX, Windows

Page 4: 为什么 rust-lang 吸引我?

背景• mozilla servo prj

• 2006~

• 2010~2011 自举• Graydon Hoare

– tracemonkey JIT backend, nanoJIT

– XPCOM cycle collector

Page 5: 为什么 rust-lang 吸引我?

hello world.

use std;

fn main(args: [str]) {

std::io::println("hello world.");

}

Page 6: 为什么 rust-lang 吸引我?

compile&run

$:rustc hello.rs

$:./hello

hello world.

Page 7: 为什么 rust-lang 吸引我?

compile error

hello.rs:2:4: 2:20 error: unresolved modulename: std

hello.rs:2 std::io::println("hello world!");

^~~~~~~~~~~~~~~~

Page 8: 为什么 rust-lang 吸引我?

实用

Page 9: 为什么 rust-lang 吸引我?

包管理与网络编程• cargo

• libuv

• 更容易调用 C code

Page 10: 为什么 rust-lang 吸引我?

丰富的类型• ()• 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

Page 11: 为什么 rust-lang 吸引我?

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 }

Page 12: 为什么 rust-lang 吸引我?

Attributes

#[cfg(target_os = "win32")]

fn register_win_service() { /* ... */ }

#[link(name = "std",

vers = "0.1",

url = "http://rust-lang.org/src/std")];

Page 13: 为什么 rust-lang 吸引我?

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());

Page 14: 为什么 rust-lang 吸引我?

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"); }

}

Page 15: 为什么 rust-lang 吸引我?

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) }

}

}

Page 16: 为什么 rust-lang 吸引我?

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);}

Page 17: 为什么 rust-lang 吸引我?

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);

• });

Page 18: 为什么 rust-lang 吸引我?

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"));

}

Page 19: 为什么 rust-lang 吸引我?

Binding

let daynum = bind vec::position_elt(["mo", "tu", "we", "do", "fr", "sa", "su"], _);

产生一个 boxed closure

Page 20: 为什么 rust-lang 吸引我?

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;

}

Page 21: 为什么 rust-lang 吸引我?

Generic datatypes

type circular_buf<T> = {start: uint,

end: uint,

buf: [mutable T]};

enum option<T> { some(T), none }

Page 22: 为什么 rust-lang 吸引我?

其它• 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

Page 23: 为什么 rust-lang 吸引我?

并发

Page 24: 为什么 rust-lang 吸引我?
Page 25: 为什么 rust-lang 吸引我?

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));

• }

Page 26: 为什么 rust-lang 吸引我?

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);

Page 27: 为什么 rust-lang 吸引我?

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;}

Page 28: 为什么 rust-lang 吸引我?

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";}

Page 29: 为什么 rust-lang 吸引我?

• A unique box type or value is constructed by the prefix tilde sigil ~.

• let x: ~int = ~10;

Page 30: 为什么 rust-lang 吸引我?

安全

下次,下次再讲。

Page 31: 为什么 rust-lang 吸引我?

Ubuntu PPA

• https://launchpad.net/~kevincantu/+archive/rust

• Ubuntu 10.04/11.10

• Thank you, Kevin Cantu.

Page 32: 为什么 rust-lang 吸引我?

http://laiyonghao.com