为什么 rust-lang 吸引我?

3,900 views

Published on

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

Published in: Technology, Business
1 Comment
4 Likes
Statistics
Notes
  • 录音:http://code.ijinshan.com/res/r/120225-gztechparty-newlang-pk/120225-gztechparty-langpk-7-rust.MP3
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
3,900
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
27
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

为什么 rust-lang 吸引我?

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

×