• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
为什么 rust-lang 吸引我?
 

为什么 rust-lang 吸引我?

on

  • 2,990 views

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

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

Statistics

Views

Total Views
2,990
Views on SlideShare
2,990
Embed Views
0

Actions

Likes
3
Downloads
24
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • 录音:http://code.ijinshan.com/res/r/120225-gztechparty-newlang-pk/120225-gztechparty-langpk-7-rust.MP3
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    为什么 rust-lang 吸引我? 为什么 rust-lang 吸引我? Presentation Transcript

    • 为什么 rust-lang 吸引我? 赖勇浩 2012.2.25
    • rust-lang
      • 实用
      • 并发
      • 安全
    • 简介
      • www.rust-lang.org
      • github.com/mozilla/rust
      • @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 [&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;] {
      • std::io::println(elt);
      • }
    • Closures
      • fn call_closure_with_ten(b: fn(int)) { b(10); }
      • let x = 20;
      • call_closure_with_ten({|arg|
      • #info(&quot;x=%d, arg=%d&quot;, 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(&quot;!&quot;);
      • std::io::println(shout(&quot;hey ho, let's go&quot;));
      • }
    • Binding
      • 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;], _);
      • 产生一个 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 = &quot;win32&quot;)]
        • #[abi = &quot;stdcall&quot;]
        • native mod kernel32 {
        • fn SetEnvironmentVariableA(n: *u8, v: *u8) -> int;
        • }
      • Tasks
      • Testing
        • #[test]
        • fn test_twice() {
        • let i = -100;
        • ...
      • > ./twice
      • running 1 tests
      • test test_twice ... ok
      • result: ok. 1 passed; 0 failed; 0 ignored
    • 并发
    •  
    • Spawning a task
      • let some_value = 22;
      • task::spawn {||
      • std::io::println(&quot;This executes in the child task.&quot;);
      • std::io::println(#fmt(&quot;%d&quot;, 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) == &quot;22&quot;;
      • }
      • 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