Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created December 7, 2015 06:49
Show Gist options
  • Save mfpiccolo/0c076e8f77a5b5b7dec3 to your computer and use it in GitHub Desktop.
Save mfpiccolo/0c076e8f77a5b5b7dec3 to your computer and use it in GitHub Desktop.
#![feature(custom_derive, core)]
extern crate core;
use std::io;
use std::io::prelude::*;
use core::fmt::Debug;
#[derive(Debug)]
struct Product {
id: i32,
name: &'static str,
label: &'static str,
}
#[derive(Debug)]
struct Label {
id: i32,
text: &'static str,
}
fn main() {
let p1 = Product {id: 1, name: "Awesome Gaget", label: "awesome"};
let p2 = Product {id: 2, name: "Cool Widget", label: "cool"};
let p3 = Product {id: 3, name: "Rad Gizmo", label: "cool"};
let p4 = Product {id: 3, name: "Lame Thingy", label: "lame"};
let products = vec![p1, p2, p3, p4];
let l1 = Label {id: 1, text: "cool"};
let l2 = Label {id: 1, text: "awesome"};
let l3 = Label {id: 1, text: "lame"};
let labels = vec![l1, l2, l3];
run_cli(&products, &labels)
}
fn run_cli(products: &Vec<Product>, labels: &Vec<Label>) {
println!("Check out these awesome products!");
let stdin = io::stdin();
for line in stdin.lock().lines() {
let l = line.unwrap();
if l == "ls products" {
print_list(products.into_iter())
} else if l == "ls labels" {
print_list(labels.into_iter())
}
}
}
fn print_list<T: Iterator<Item=I>, I: Debug>(list: T) {
for item in list {
println!("{:?}", item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment