Skip to content

Instantly share code, notes, and snippets.

@arkadijs
Created June 27, 2015 16:07
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 arkadijs/9a45f8927777f2726197 to your computer and use it in GitHub Desktop.
Save arkadijs/9a45f8927777f2726197 to your computer and use it in GitHub Desktop.
Beauty of Rust
#![feature(convert, slice_patterns)]
#![allow(unused_variables)]
// http://pzol.github.io/getting_rusty/posts/20140417_destructuring_in_rust/
use std::env;
use std::io::BufReader;
use std::io::BufRead;
use std::fs::File;
fn main() {
let model = "Work/opengl/models/african_head.obj";
let mut path = env::home_dir()
.unwrap_or_else(|| panic!("Cannot open {}: $HOME not set", model));
path.push(model);
let filename = path.to_str().unwrap();
let file = File::open(path.as_path())
.unwrap_or_else(|err| panic!("Cannot open {}: {}", filename, err));
let reader = BufReader::new(file);
let mut v = 0usize;
let mut t = 0usize;
// struct std::io::Lines impl Iterator -> type std::io::Result<String> ->
// enum std::result::Result<String, Error> -> Ok, Err
for maybe in reader.lines() { // for Ok(line) ... /*
match maybe {
Err(err) => panic!("I/O error reading {}: {}", filename, err),
Ok(line) => {
// struct std::str::SplitWhitespace impl Iterator -> collect<T> -> T (Vec<&str>)
match line.split_whitespace().collect::<Vec<_>>().as_slice() {
["v", x, y, z] => v += 1,
["vt", u, v, _] => t += 1,
_ => () ,
}
}
}
}
println!("\nvertices: {}; texture coords: {}", v, t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment