Skip to content

Instantly share code, notes, and snippets.

Graham King grahamking

Block or report user

Report or block grahamking

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@grahamking
grahamking / slicemap_test.go
Created Jun 30, 2015
Benchmark comparing map access vs slice search
View slicemap_test.go
package main
import (
"math/rand"
"testing"
"time"
)
const (
numItems = 100 // change this to see how number of items affects speed
View online_upgrade.go
package main
import (
"bytes"
"encoding/gob"
"fmt"
"net"
"os"
"os/exec"
"os/signal"
@grahamking
grahamking / m1.rs
Created Apr 29, 2013
Using a module
View m1.rs
mod m2;
fn main() {
m2::say_hi();
}
@grahamking
grahamking / m2.rs
Created Apr 29, 2013
A module to use from a different file
View m2.rs
pub fn say_hi() {
println("Hi!");
}
@grahamking
grahamking / objects.rs
Last active Dec 16, 2015
Rust: Objects
View objects.rs
extern mod std;
use std::time;
struct User {
name: ~str,
age: int
}
impl User {
@grahamking
grahamking / mutable_pointers.rs
Last active Dec 16, 2015
Rust: The pointer and the box it points to
View mutable_pointers.rs
struct Point { x: int, y:int }
fn main() {
// Local variable, easy
let mut p1 = Point{x:1, y:2};
p1.x = 10;
// Owned pointer
// also easy because target inherts mutability
@grahamking
grahamking / memory_example.rs
Last active Dec 16, 2015
Rust: Owner memory example
View memory_example.rs
fn main() {
let name: ~str,
other: ~str;
name = ~"Bob";
other = name;
println(other);
@grahamking
grahamking / use_sqlite.rs
Last active Dec 16, 2015
Rust: Use sqlite
View use_sqlite.rs
extern mod sqlite;
fn db() {
let database =
match sqlite::open("test.db") {
Ok(db) => db,
Err(e) => {
println(fmt!("Error opening test.db: %?", e));
return;
@grahamking
grahamking / net_fetch.rs
Created Apr 26, 2013
Rust: Connect to a socket and talk
View net_fetch.rs
extern mod std;
use std::{net_tcp,net_ip};
use std::uv;
fn fetch(code: ~str) -> ~[~str] {
let ipaddr = net_ip::v4::parse_addr("205.156.51.232");
let iotask = uv::global_loop::get();
let connect_result = net_tcp::connect(ipaddr, 80, &iotask);
@grahamking
grahamking / load_file_2.rs
Created Apr 26, 2013
Rust: Load a file v2
View load_file_2.rs
fn load(filename: ~str) -> ~[~str] {
// The simple way:
// let read_result = io::file_reader(~path::Path(filename));
let read_result: Result<@Reader, ~str>;
read_result = io::file_reader(~path::Path(filename));
match read_result {
Ok(file) => return file.read_lines(),
You can’t perform that action at this time.