Skip to content

Instantly share code, notes, and snippets.

View bvssvni's full-sized avatar

Sven Nilsen bvssvni

View GitHub Profile
rustc 0.10-pre (0a5138c 2014-03-04 13:16:41 -0800)
/// A struct with borrowed pointer to a closure.
/// This is to be able to reuse the closure then struct runs out of scope.
pub struct Test<'a> {
f: &'a 'a ||
}
impl<'a> Test<'a> {
rustc 0.10-pre (0a5138c 2014-03-04 13:16:41 -0800)
pub trait Expr<'a, T> {
fn then(&'a self, f: &'a 'a ||) -> T;
}
impl<'a> Expr<'a, ()> for () {
fn then(&'a self, f: &'a 'a ||) -> () {
(*f)();
@bvssvni
bvssvni / gist:9408082
Last active August 29, 2015 13:57
A 'rusti' interpreter script (by mcpherrin)
#!/bin/sh
while true; do
echo -n " > "
read line
TMP=`mktemp`
rustc - -o $TMP <<EOF
#[feature(globs, macro_rules, struct_variant)];
// extern mod extra;
fn main() {
/**
* cairo_show_text:
* @cr: a cairo context
* @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
*
* A drawing operator that generates the shape from a string of UTF-8
* characters, rendered according to the current font_face, font_size
* (font_matrix), and font_options.
*
let x = Some(1);
let y = Some(2);
let z = monad!(
None {
_ => x,
Some(x) => y,
Some(y) => Some(x+y)
}
);
assert_eq!(z, Some(3));
enum Void {}
enum Foo<T, U> {
A(int) for T = (), U = ()
}
fn insert_text(text: &mut HashMap<&'static str, &'static str>) {
let text: Vec<&'static str> = vec!(include!("text.rs"));
for chunk in text.as_slice().chunks(2) {
let id: &'static str = *chunk.get(0).unwrap();
let txt: &'static str = *chunk.get(1).unwrap();
text.insert(id, txt);
}
}
// rustc 0.10-pre-nightly (7bda3df 2014-04-02 17:51:48 -0700)
// host: x86_64-apple-darwin
//
// Compile with
// RUST_BACKTRACE=1 rustc lib.rs --crate-type=rlib
#![allow(unreachable_code)]
pub fn foo() {
fail!("hello");
@bvssvni
bvssvni / gist:10579832
Created April 13, 2014 11:23
Idea: Type match in Rust
/// Returns true if T is int, otherwise false.
fn is_int<T: Num>(num: T) -> bool {
match <T> {
int => true,
_ => false,
}
}
/// Returns true if T impls Clone.
fn is_clone(val: T) -> bool {
compute : Integer -> Integer
compute x = if (x `mod` 2) == 0 then x `div` 2 else 3 * x + 1
traverse : Integer -> List Integer
traverse 1 = [1]
traverse x = traverse (compute x) :: x
ERROR:
collatz.idr:7:35: