Skip to content

Instantly share code, notes, and snippets.

@quux00
quux00 / atomicbooltest.rs
Created January 24, 2014 03:15
rust enums
use std::sync::atomics::AtomicBool;
use std::sync::atomics::Ordering;
// error unresolved import: there is no `Relaxed` in `std::sync::atomics::Ordering`
// use std::sync::atomics::Ordering::Relaxed;
fn main() {
let mut ab = AtomicBool::new(false);
let val1 = ab.load(Ordering::Relaxed); // error: unresolved import:
// there is no `Relaxed` in `std::sync::atomics::Ordering`
@quux00
quux00 / telephonic-whispers-with-maxprocs.go
Created January 26, 2014 17:15
Telephonic whispers with GOMAXPROCS set in Go.
package main
import (
"fmt"
"runtime"
)
func whisper(left, right chan int) {
left <- 1 + <- right
}
use std::io::File;
use std::str;
fn main() {
let file_name_abs = ~"foo";
let mut response =~ "";
match File::open(&Path::new(file_name_abs)) {
Some(html_file) => {
let mut html_file_mut = html_file;
@quux00
quux00 / hof-reordered.rs
Last active January 4, 2016 21:19
Hof in Rust
fn double(a: int) -> int { a * 2 }
fn ntimes(f: proc(int) -> int, times: int) -> proc(int) -> int {
proc(x: int) {
match times {
0 => { x },
_ => { f(ntimes(f, times - 1)(x)) } // swap the order relative to the original example
}
}
}
My measurements for the "kernel interrupt timing" programs:
|--------------------+-----------------+-------------+------------------------------|
| SIGALRM Interval | Program version | Measured Hz | Measured Interval Avg (usec) |
|--------------------+-----------------+-------------+------------------------------|
| (init setting) 250 | Rust | 4001 | 250 |
| (init setting) 250 | C | 4016 | 249 |
|--------------------+-----------------+-------------+------------------------------|
| 500 | Rust | 2001 | 500 |
| 500 | C | 2004 | 499 |
use std::io::process;
use std::io::process::{Process, ProcessConfig};
use std::comm::{Port, Chan};
fn main() {
let (port, chan) = Chan::new();
spawn(proc(){taskEntry(port)});
chan.send(~"ls");
}
@quux00
quux00 / snippet.rs
Last active August 29, 2015 13:56
Snippet of web server code in Rust
use std::io::EndOfFile;
use std::io::io_error;
// ...
fn respond_with_static_file(stream: Option<std::io::net::tcp::TcpStream>, path: &Path) {
let mut stream = stream;
let sz = 16384;
let result = io::result(|| File::open(path));
@quux00
quux00 / refer.rs
Last active August 29, 2015 13:56
Reference functions in an impl
struct MyThing {
val: int
}
impl MyThing {
fn remove_first(s: &str) -> ~str {
s.slice_from(1).to_owned()
}
fn remove_last(&self, s: &str) -> ~str {
@quux00
quux00 / memcached.rs
Last active August 29, 2015 13:56
How to import C enum into Rust?
// Rust code
use std::libc;
use std::ptr;
struct memcached_st;
struct memcached_server_st;
struct memcached_server_list_st;
type in_port_t = i32;
#[repr(C)]
use std::str::StrVector;
struct A<'a> {
field: &'a StrVector
}
fn main() {
let vs = ~[~"one", ~"two", ~"three"];
let cc = vs.concat();
println!("{:?}", cc); // works