Skip to content

Instantly share code, notes, and snippets.

@nikomatsakis
Created August 16, 2015 12:24
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 nikomatsakis/3533ffa25b6c07ac4636 to your computer and use it in GitHub Desktop.
Save nikomatsakis/3533ffa25b6c07ac4636 to your computer and use it in GitHub Desktop.
#![feature(rustc_attrs)]
#![feature(slice_patterns)]
#![allow(dead_code)]
#[rustc_mir(graphviz="sum-with-overloaded-index.dot")]
fn sum_with_overloaded_index(x: Vec<i32>) -> i32 {
let mut i = 0;
let mut sum = 0;
let l = x.len();
while i < l {
sum += x[i];
i += 1;
}
sum
}
#[rustc_mir(graphviz="sum-with-while-loop.dot")]
fn sum_with_while_loop(x: &[i32]) -> i32 {
let mut i = 0;
let mut sum = 0;
let l = x.len();
while i < l {
sum += x[i];
i += 1;
}
sum
}
#[rustc_mir(graphviz="option-or-else.dot")]
fn option_or_else<T:Clone>(x: (&Option<T>, &Option<T>)) -> Option<T> {
match x {
(&Some(ref v), _) => Some(v.clone()),
(&None, &Some(ref u)) => Some(u.clone()),
(&None, &None) => None,
}
}
#[rustc_mir(graphviz="subrange.dot")]
fn subrange<T>(slice: &[T], a: usize, b: usize) {
let _s1 = &slice[a..];
let _s2 = &slice[..b];
let _s3 = &slice[a..b];
}
#[rustc_mir(graphviz="dot_product.dot")]
fn scaled_dot_product(scale: i32, vec1: &[i32], vec2: &[i32]) -> i32 {
vec1.iter()
.zip(vec2)
.map(|(&e1, &e2)| scale * e1 * e2)
.fold(0, |a, b| a + b)
}
#[rustc_mir(graphviz="vec_pattern.dot")]
fn vec_pattern(vec1: &[i32]) {
match vec1 {
[1] => println!("[1]"),
[2] => println!("[2]"),
[x] => println!("[x={:?}]", x),
[x, y] => println!("[x={:?}, y={:?}]", x, y),
z => println!("z={:?}", z),
}
}
enum Type {
// note: order of variants is inverse of order below
TyEnum(usize, u32),
TyStruct(usize, u32),
TyOther
}
#[rustc_mir(graphviz="vec_pattern.dot")]
fn guards(v: Type) {
match v {
// test a tricky case where order of variants doesn't match,
// which reveals the fact that the two defs below have distinct
// nodeids
Type::TyStruct(def, _) |
Type::TyEnum(def, _) if def == 0 => {
}
_ => { }
}
}
fn main() { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment