Skip to content

Instantly share code, notes, and snippets.

@grahamking
grahamking / mutable_pointers.rs
Last active December 16, 2015 17:29
Rust: The pointer and the box it points to
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 / objects.rs
Last active December 16, 2015 17:29
Rust: Objects
extern mod std;
use std::time;
struct User {
name: ~str,
age: int
}
impl User {
@grahamking
grahamking / m2.rs
Created April 29, 2013 21:14
A module to use from a different file
pub fn say_hi() {
println("Hi!");
}
@grahamking
grahamking / m1.rs
Created April 29, 2013 21:16
Using a module
mod m2;
fn main() {
m2::say_hi();
}
package main
import (
"bytes"
"encoding/gob"
"fmt"
"net"
"os"
"os/exec"
"os/signal"
@grahamking
grahamking / send.go
Last active August 29, 2015 14:14
ICMP raw send
package main
import (
"log"
"net"
"syscall"
)
func main() {
var err error
@grahamking
grahamking / recv.go
Last active August 29, 2015 14:14
ICMP raw receive
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
fd, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP)
@grahamking
grahamking / slicemap_test.go
Created June 30, 2015 04:11
Benchmark comparing map access vs slice search
package main
import (
"math/rand"
"testing"
"time"
)
const (
numItems = 100 // change this to see how number of items affects speed
.text
.global _start
_start:
mov $12, %rax # brk syscall number
mov $0, %rdi # 0 is invalid, want to get current position
syscall
mov %rax, %rsi # rsi now points to start of heap mem we'll allocate
@grahamking
grahamking / errs.go
Created May 22, 2020 17:09
Collect and handle multiple errors in Go
package util
import (
"errors"
"strings"
)
// Errs is an error that collects other errors, for when you want to do
// several things and then report all of them.
type Errs struct {