This file contains hidden or 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; | |
| import result::{result, ok, chain}; | |
| fn read_file(filename: str) -> result<@str, @str> { | |
| ret ok(@("<contents of " + filename + ">")); | |
| } | |
| fn parse_buf(buf: @str) -> @str { | |
| ret @("<parsed: " + *buf + ">"); | |
| } |
This file contains hidden or 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 try<T: Send>(f: ~fn(~fn(~str) -> !) -> T) -> Result<T, ~str> { | |
| let (wr, rd) = task::stream(); | |
| let result: Result<T, ()> = do tasks::try |move wr| { | |
| let fail_: fn~(~str) -> ! = fn~(x: ~str) -> ! { wr.send(x); fail; }; | |
| f(fail_) | |
| }; | |
| return match move result { | |
| Ok(move v) => Ok(move v), | |
| Err(_) => match rd.try_recv() { | |
| Some(move s) => Err(move s), |
This file contains hidden or 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 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; } |
This file contains hidden or 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 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; | |
| } |
This file contains hidden or 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 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)); |
This file contains hidden or 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
| #include <utility> | |
| template<typename T> | |
| void f() { | |
| struct S { | |
| void g(int a) { | |
| g(0); | |
| } | |
| }; | |
| S s; |
This file contains hidden or 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
| #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) { |
This file contains hidden or 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 { | |
| x: int | |
| } | |
| impl Foo: Drop { | |
| fn finalize() { | |
| io::println(fmt!("drop: %?", ptr::to_unsafe_ptr(&self))); | |
| } | |
| } |
This file contains hidden or 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 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)); | |
| } |
This file contains hidden or 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
| 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(); | |
| } |