Skip to content

Instantly share code, notes, and snippets.

struct S<'self> {
v: &int
}
fn f<'lt>(_s: &S<'lt>) {}
fn main() {
f(& S { v: &42 });
}
@ben0x539
ben0x539 / foo.rs
Last active December 14, 2015 07:29
struct Vec<'self, T> {
vec: &'self [T]
}
fn advance<'lt, T>(w: &mut Vec<'lt, T>) {
w.vec = vec::slice(w.vec, 1, w.vec.len());
}
fn wrap<'lt, T>(v: &'lt [T]) -> Vec<'lt, T> {
Vec { vec: v }
trait InputRange<T> {
pure fn is_empty(&self) -> bool;
pure fn head(&self) -> &self/T;
pure fn tail(&self) -> Self;
}
pure fn each<T, U: InputRange<T>>(range: U, blk: fn(v: &T) -> bool) {
while !range.is_empty() && blk(range.head()) {
range = range.tail();
}
use core::iter::BaseIter;
use io::ReaderUtil;
fn main() {
let va = range_iter(1, 10);
let vb = ReaderIter { reader: io::stdin() };
for both(&va, &vb).each |&(a, b)| {
io::println(fmt!("%d, %s", a, b));
}
struct Foo {
x: int
}
impl Foo: Drop {
fn finalize() {
io::println(fmt!("drop: %?", ptr::to_unsafe_ptr(&self)));
}
}
#include <iostream>
#include <boost/asio.hpp>
#include <boost/system/system_error.hpp>
#include <unistd.h>
namespace asio = boost::asio;
template<typename Lambda>
void fork_with_pipe(asio::io_service& io, Lambda&& on_line_read) {
#include <utility>
template<typename T>
void f() {
struct S {
void g(int a) {
g(0);
}
};
S s;
use libc::{c_void, size_t};
#[nolink]
extern mod libc_ {
fn qsort(base: *libc::c_void, nmemb: size_t, size: size_t,
compar: *u8); // extern fn(a: *c_void, b: *c_void) -> int
}
fn compare<T: cmp::Ord>(a: *c_void, b: *c_void) -> int unsafe {
let (a_, b_) = (&*(a as *T), &*(b as *T));
fn read_struct<R: io::Reader, T>(reader: R, obj: &mut T) {
let size: uint = sys::size_of::<T>();
unsafe {
let borrowed_ptr: &u8 = cast::reinterpret_cast(&obj);
let ptr: *u8 = ptr::to_unsafe_ptr(borrowed_ptr);
do vec::raw::buf_as_slice(ptr, size) |buf: &[u8]| {
let mut_buf: &[mut u8] = cast::reinterpret_cast(&buf);
let ret = reader.read(mut_buf, size);
assert ret == size;
}
use io::ReaderUtil;
fn main() {
io::println("What is your favorite number?");
let my_favorite_number = (fn() -> int {
for io::stdin().each_line |line| {
match int::from_str(line) {
None => { io::println("That is not a number. :(\nTry again!"); }
Some(number) => { return number; }