Skip to content

Instantly share code, notes, and snippets.

@jmgrosen
Created July 4, 2013 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmgrosen/5930078 to your computer and use it in GitHub Desktop.
Save jmgrosen/5930078 to your computer and use it in GitHub Desktop.
use std::vec;
struct IndexableThing;
impl Index<uint, ~str> for IndexableThing {
fn index(&self, rhs: &uint) -> ~str {
(*rhs).to_str()
}
}
impl Index<uint, ~[uint]> for IndexableThing {
fn index(&self, rhs: &uint) -> ~[uint] {
vec::from_fn(*rhs, |i| i)
}
}
fn main() {
let thing = IndexableThing;
let thing_str: ~str = thing[5];
println(thing_str);
let thing_vec: ~[uint] = thing[5];
println(fmt!("%?", thing_vec));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment