Skip to content

Instantly share code, notes, and snippets.

## Dereferencing pointers
Rust uses the unary star operator (`*`) to access the contents of a box or pointer, similarly to C.
~~~
let managed = @10;
let owned = ~20;
let borrowed = &30;
let sum = *managed + *owned + *borrowed;
trait Positioned {
fn get_x(&self) -> int;
fn get_y(&self) -> int;
fn set_x(&self, x: int);
fn set_y(&self, y: int);
}
trait Movable: Positioned {
fn translate(&self, dx: int, dy: int) {
self.set_x(self.get_x() + dx);
14:36 <@brson> ldleworker: it is a possibility and I think it's been discussed before. there is a somewhat similar
notion in the air for accessing common fields of case classes. personally, for this particular case
the implementation of `impl Entity : Positioned` is so obvious that struct embedding seems like
more than necessary. A simple syntactic transformation would do it, and that's why deriving impls
makes more sense to me. (wrt deriving, that is how we expect to implement things like Eq, which are
all boilerplate)
fn main() {
let (spawn_chan, spawn_port) = pipes::stream();
// Create a 'spawn service' on this scheduler
do spawn |move spawn_port| {
loop {
let next_spawnee = spawn_port.recv();
spawn(next_spawnee);
}
pub trait BaseIter<A> {
pure fn each(&self, blk: fn(v: &A) -> bool);
pure fn size_hint(&self) -> Option<uint>;
}
pub trait ExtendedIter<A> : BaseIter<A> {
pub trait BaseIter<A> {
pure fn each(&self, blk: fn(v: &A) -> bool);
pure fn size_hint(&self) -> Option<uint>;
}
pub trait ExtendedIter<A> : BaseIter<A> {
extern mod std;
use core::pipes::*;
use std::comm::DuplexStream;
use core::task;
fn main () {
let (left_chan, right_port): (Chan<~str>, Port<~str>) = stream();
let (right_chan, left_port): (Chan<int>, Port<int>) = stream();
do task::spawn() |move left_chan, move left_port| {
extern mod std;
use core::pipes::*;
use std::comm::DuplexStream;
use core::task;
fn main () {
let (chan, port): (Chan<int>, Port<int>) = stream();
do task::spawn() |move port| {
assert port.recv() == 123;
pub pure fn get_default_ref<T>(opt: &r/Option<T>, def: &r/T) -> &r/T {
match *opt {
Some(ref x) => x,
None => def
}
}
fn main() {
match os::homedir() {
None => {
use std::comm::DuplexStream;
enum PingPong { Ping, Pong };
fn main() {
let (channel1, channel2): (DuplexStream<PingPong, PingPong>, DuplexStream<PingPong, PingPong>) = DuplexStream();
spawn |move channel1| {
channel.send(Ping),
loop {