11. “The most likely way for the
world to be destroyed, most
experts agree, is by accident.
That's where we come in; we're
computer professionals. We
cause accidents.”
- Nathaniel Borenstein
71. use core::rand::RngUtil;
fn main() {
for ["Alice", "Bob", "Carol"].each |&name| {
do spawn {
let v = rand::Rng().shuffle([1, 2, 3]);
for v.each |&num| {
print(fmt!("%s says: '%d'n", name, num))
}
}
}
}
72. Type
Memory safety
Concurrency
Generics
Exception handling
Memory model
Compilation model
system static, nominal, linear, algebraic, locally inferred
no null or dangling pointers, no buffer overflows
lightweight tasks with message passing, no shared memory
type parameterization with type classes
unrecoverable unwinding with task isolation
optional task-local GC, safe pointer types with region analysis
ahead-of-time, C/C++ compatible
73. let hi = "hi";
let mut count = 0;
while count < 10 {
io::println(fmt!("count: %?", count));
count += 1;
}
Imutável por default
74. fn angle(vector: (float, float)) -> float {
let pi = float::consts::pi;
match vector {
(0f, y) if y < 0f => 1.5 * pi,
(0f, y) => 0.5 * pi,
(x, y) => float::atan(y / x)
}
}
Pattern Matching
75. struct Point {
x: float,
y: float
}
let mut mypoint = Point { x: 1.0, y: 1.0 };
let origin = Point { x: 0.0, y: 0.0 };
mypoint.y += 1.0; // mypoint is mutable, and its fields as well
origin.y += 1.0; // ERROR: assigning to immutable field
Structs
76. fn line(a: int, b: int, x: int) -> int
{
a * x + b
}
Funções
77. Owning Pointers (~)
fn f() {
let x: ~int = ~1024; // allocate space and initialize an int
// on the heap
println(fmt!("%d", *x));
} // <-- the memory that x pointed at is automatically freed here
let x = ~5;
let z = x; // no new memory allocated, x can no longer be used
78. fn foo() {
let x: @int = @1024; // allocate space and initialize an int
// on the heap
bar(x); // pass it to `bar`
println(fmt!("%d", *x)); // print it on the screen
} // <-- the memory can be freed here
fn bar(x: @int) {
let y: @int = x; // make a new smart pointer to `x`
} // <-- despite `y` going out of scope,the memory is *not* freed here
Managed Pointers (@)
79. fn dogshow() {
let dogs: [~Dog * 3] = [
~Dog { name: ~"Spot" },
~Dog { name: ~"Fido" },
~Dog { name: ~"Snoopy" },
];
let winner: &Dog = dogs[1];// note use of `&` to form a reference
for dogs.each |dog| {
println(fmt!("Say hello to %s", dog.name));
}
println(fmt!("And the winner is: %s!", winner.name));
} // <-- all dogs destroyed here
Borrowed Pointers (&)
80. Freezing
let mut x = 5;
{
let y = &x; // x is now frozen, it cannot be modified
}
// x is now unfrozen again
81. use core::task::spawn;
// Print something profound in a different task using a named function
fn print_message() { println("I am running in a different task!"); }
spawn(print_message);
// Print something more profound in a different task using a lambda expression
spawn( || println("I am also running in a different task!") );
// The canonical way to spawn is using `do` notation
do spawn {
println("I too am running in a different task!");
}
Tasks