Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am kenpratt on github.
  • I am kenpratt (https://keybase.io/kenpratt) on keybase.
  • I have a public key whose fingerprint is DEB1 C9E5 A9CB 6FF9 0D43 6DA8 F9D6 133C F21F 6CD3

To claim this, I am signing this object:

@kenpratt
kenpratt / trait_in_enum_with_box_and_refs.rs
Created May 21, 2015 04:05
Trying to use a trait as a field in an enum
trait Polygon {
fn area(&self) -> f32;
}
struct Circle<'a> {
radius: &'a f32,
}
impl<'a> Polygon for Circle<'a> {
fn area(&self) -> f32 {
@kenpratt
kenpratt / trait_in_enum.rs
Created May 21, 2015 03:54
Trying to use a trait as a field in an enum
trait Polygon {
fn area(&self) -> f32;
}
struct Circle {
radius: f32,
}
impl Polygon for Circle {
fn area(&self) -> f32 {
enum Test {
One(String),
Two(String),
Three(uint),
Four(bool)
}
fn check(s: Test) {
match s {
Some("x") => println!("got an x"),
@kenpratt
kenpratt / match_string.rs
Last active August 29, 2015 14:03
Trying to match String content inside an Enum.
// works, but ugly
fn check1(s: Option<String>) {
match s {
Some(t) => {
match t.as_slice() {
"test" => println!("yep"),
_ => println!("nope")
}
},
None => println!("nope")
#[deriving(Show)]
struct SampleError<'a> {
message: &'a str
}
fn generate_err() -> Result<(), SampleError> {
let m = format!("sample dynamic error: {}", 42);
Err(SampleError { message: m.as_slice() })
}
#[deriving(Show)]
enum Token<'a> {
Integer(int),
String(&'a str)
}
fn generate_int() -> Token {
Integer(23)
}
fn generate() -> &str {
let s = format!("meaning of life: {}", 42);
s.as_slice()
}
fn main() {
println!("{}", generate());
}
/*
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Tahoma, Geneva, sans-serif;
}
div {
display: inline;
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8088})
, _ = require("underscore");
var connections = [];
wss.on('connection', function(ws) {
connections.push(ws);
ws.on('message', function(message) {
console.log('received: %s', message);