This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(macro_rules)] | |
macro_rules! foo { | |
($head:expr, $($tail:expr),+) => ({ | |
println!("{}", $head); | |
foo!($($tail),+); | |
}); | |
($last:expr) => ({ | |
println!("{}", $last); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19:45 < Guest4321> @kmc: Assuming Rust has HKT, why not just define a Functor as a higher-kinded struct and define a generic impl on | |
it? Why do we need traits? | |
19:45 < Guest4321> had* | |
19:46 < frozendevil!Adium@moz-62FB197F.com [Quit: Leaving.] | |
19:46 < mcpherrin> kmc: https://github.com/mozilla/rust/issues/6393 | |
19:46 <@cmr> Guest4321: functor describes behavior of an interface, not a single concrete thing. | |
19:46 <@kmc> Guest4321: because the implementation of fmap varies according to the type inside | |
19:46 <@cmr> that'd be like making a monoid struct | |
19:46 <@cmr> what would the point be | |
19:47 <@kmc> it's not parametric polymorphism |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Foo::Bar; | |
mod Foo { | |
pub struct Bar { | |
pub x: int, | |
y: int | |
} | |
impl Bar { | |
pub fn new(new_x: int, new_y: int) -> Bar { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Foo<T> { | |
x: i16, | |
y: T | |
} | |
fn bar<T>(foo: Foo<T>) -> i16 { | |
foo.x | |
} | |
fn main() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::iter::{Iterator, FromIterator}; | |
fn map<T, IT: Iterator<T>, U, IU: FromIterator<U>>(fun: |T|->U, coll: IT) -> IU { | |
let mut acc = Vec::new(); | |
for elt in coll.move_iter() { // error: type `IT` does not implement any method in scope named `move_iter` | |
acc.push(fun(elt)); | |
} | |
acc.move_iter().collect() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn map<T, U>(fun: |T|->U, coll: ~[T]) -> ~[U] { | |
let mut acc = Vec::new(); | |
for elt in coll.move_iter() { | |
acc.push(fun(elt)); | |
} | |
acc.move_iter().collect() | |
} | |
fn main() { | |
let x = map(|x| x+2, ~[1u,2,3]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn foo<T, U>(x: T) -> U { | |
x // error: mismatched types: expected `U` but found `T` (expected type parameter but found type parameter) | |
} | |
fn main() { | |
foo(1u); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
repr.rs:5:5: 5:15 error: discriminant value outside specified type | |
repr.rs:5 Qux = 5000 | |
^~~~~~~~~~ | |
repr.rs:1:8: 1:10 note: discriminant type specified here | |
repr.rs:1 #[repr(i8)] | |
^~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::f32::consts::PI; | |
use std::io::{BufferedWriter, stdout}; | |
use std::rand::{Rng, StdRng}; | |
struct Vec2 { | |
x: f32, | |
y: f32, | |
} | |
fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut pixels = [0f32, ..8]; | |
for &mut pixel in pixels.mut_iter() { | |
pixel = 2.0; | |
} | |
println!("{:?}", pixels); // [0f32, 0f32, 0f32, 0f32, 0f32, 0f32, 0f32, 0f32] | |
} |