Skip to content

Instantly share code, notes, and snippets.

View bstrie's full-sized avatar

bstrie bstrie

View GitHub Profile
rust-git
[thestinger]
SigLevel = Optional
Server = http://pkgbuild.com/~thestinger/repo/$arch
fn main() {
let mut w = std::io::BufferedWriter::new(std::io::stdio::stdout());
for _ in range(0, 256) {
w.write_char('b');
}
w.flush();
}
fn main() {
println!("{:i}", sum([1,2,3,4,5]));
}
fn sum(x: &[int]) -> int {
match x {
[head, ..tail] => sum(tail) + head,
[] => 0
}
}
use std::iter::Unfold;
fn main() {
for fib in (0,1).unfold(next).take(20) {
println!("{}", fib);
};
}
fn next(arg: &mut (int, int)) -> Option<int> {
let (lo, hi) = *arg;
use std::iter::Unfold;
fn main() {
for fib in next.unfold((0,1)).take(20) {
println!("{}", fib);
};
}
fn next(arg: &mut (int, int)) -> Option<int> {
let (lo, hi) = *arg;
use std::iter::Unfold;
fn next(arg: &mut (int, int)) -> Option<int> {
let (lo, hi) = *arg;
*arg = (hi, lo + hi);
Some(lo)
}
fn main() {
for fib in Unfold::new((0,1), next).take(20) {
struct Foo {
a: (int, int)
}
fn main() {
let foo = Foo { a: (1, 2) };
let Foo { a: (x, y) } = foo;
println!("{} {}", x, y);
}
@bstrie
bstrie / wiki5.rs
Last active January 4, 2016 01:38
/* This program defines a recursive datastructure and implements methods upon it.
Recursive datastructures require a layer of indirection, which is provided here
by a unique pointer, indicated by the tilde `~` operator. These are analogous to
the C++ library type `std::unique_ptr`, though with more static safety guarantees. */
fn main() {
let list = ~Node(1, ~Node(2, ~Node(3, ~Empty)));
println!("Sum of all values in the list: {:i}.", list.multiply_by(2).sum());
}
// An `enum` defines a type that may be one of several different kinds of values at runtime.
fn main() {
let argv = std::os::args();
let (x, y, z) = match argv {
[filename, arg1, arg2, arg3, ..rest] => (arg1, arg2, arg3),
_ => fail!("At least three arguments expected.")
};
}
fn integral(f: &|f32|->f32, p: u32, a: f32, b: f32) -> f32 {
if p == 1 {
(b-a) * ((*f)(a) + 4.0 * (*f)((a+b)/2.0) + (*f)(b))/6.0
}
else {
let mid = (a+b)/2.0;
integral(f, p-1, a, mid) + integral(f, p-1, mid, b)
}
}