Skip to content

Instantly share code, notes, and snippets.

@KodrAus
KodrAus / playground.rs
Last active August 21, 2016 10:29
Rust Generics
#[derive(Debug)]
struct Person<T> {
id: T,
title: String
}
impl <T> Person<T> {
fn new<I: Into<String>>(id: T, title: I) -> Self {
Person {
id: id,
@KodrAus
KodrAus / playground.rs
Last active August 22, 2016 10:05
Rust Modules
mod entities {
//This module is public to anyone outside entities
pub mod traits {
pub trait Entity {
fn id(&self) -> i32;
fn title(&self) -> &str;
}
}
//This module is private to anyone outside entities
@KodrAus
KodrAus / playground.rs
Last active August 20, 2016 09:58
Rust Traits
struct Person {
id: i32,
title: String
}
impl Person {
fn new(id: i32, title: &str) -> Person {
Person {
id: id,
title: title.to_string()
@KodrAus
KodrAus / playground.rs
Last active August 22, 2016 10:26
Rust Results
fn maybe_works(i: i32) -> Result<(), String> {
//The try! macro makes early return from errors easier. There's a '?' operator coming soon
//Composing error types and implementing Error can suck, so libraries like error_chain help
//Result has similar helpers to Option
try!(must_not_be_negative(i));
Ok(())
}
@KodrAus
KodrAus / playground.rs
Last active August 22, 2016 09:59
Rust Lifetimes
//Lifetimes are generic parameters
//'a and 'b are lifetimes
//'static is a special lifetime for static memory
struct Person<'a> {
pub id: i32,
//Question: can we mutate parent.unwrap().id?
pub parent: Option<&'a Person<'a>>
}
fn main() {
@KodrAus
KodrAus / playground.rs
Last active August 22, 2016 10:28
Rust References
fn main() {
//String is a (possibly) mutable, growable slice of UTF-8 characters
let mut mutable_string: String = String::from("my owned string");
//We can borrow a String as an immutable &str, because of the Deref trait
let borrowed_str: &str = &mutable_string;
//Vec is a (possibly) mutable, growable contiguous array of things
let mut mutable_vec: Vec<i32> = vec![1, 2, 3];
@KodrAus
KodrAus / playground.rs
Last active August 22, 2016 10:00
Rust Structures
struct Person {
pub id: i32,
pub title: String
}
fn main() {
//structs are constructed inline. We could use a static method
//Rust structures have completely separate data and functionality blocks
let person = Person {
id: 1,
@KodrAus
KodrAus / playground.rs
Last active August 17, 2016 11:03
Rust Helloworld
fn main() {
println!("Hello, World!");
}
@KodrAus
KodrAus / Program.cs
Created July 10, 2016 09:37
Domain with Explicitly-typed Child Collections
/*
Entity Framework has a problem: there is 1 object graph to rule them all, and that object graph is available
to anybody, whether or not they actually want/need it. This makes things ambiguous and leads to errors and
crap code.
It's saner to be able to constrain the data you read/write to exactly what you expect to work with. But
this fragments your entities. The idea here is to try and get the best of both worlds; a single base definition
of a given entity, but only exposing the bits that are relevant to a particular projection.
@KodrAus
KodrAus / Program.cs
Created October 28, 2015 09:42
mono Orleans Output
using System;
using System.Net;
using Orleans;
using Orleans.Runtime.Host;
using OrleansDemo.GrainInterfaces;
using OrleansDemo.GrainCollection;
namespace OrleansDemo.Host