xref: /OpenGrok/opengrok-indexer/src/test/resources/sources/rust/main.rs (revision 2bcacabbe843448903326d34ff21a265b5f37596)
1 //! opengrok rust test by Nikolay Denev <ndenev@gmail.com>
2 
3 //
4 // Random Rust source
5 //
6 
7 /// Count enum
8 enum Count {
9     One,
10     Two(u64),
11     Three(String),
12     Four { id: u64, name: String },
13 }
14 
15 struct Counter<T> where T: ShowItem {
16     item: T,
17 }
18 
19 impl<T> Counter<T> where T: ShowItem {
new(item: T) -> Counter<T>20     fn new(item: T) -> Counter<T> {
21         Counter { item: item }
22     }
23 }
24 
25 /// Trait
26 trait ShowItem {
show(&self) -> String27     fn show(&self) -> String;
28 }
29 
30 /// Trait implementation
31 impl ShowItem for Count {
show(&self) -> String32     fn show(&self) -> String {
33         match *self {
34             Count::One => "One!".to_string(),
35             Count::Two(i) => format!("Two: {}!", i),
36             Count::Three(ref s) => format!("Three: {}!", s),
37             Count::Four { id: i, name: ref s } => format!("Four: {}, {}!", i, s),
38         }
39     }
40 }
41 
42 // fn main()
main()43 fn main() {
44     let mut c: Vec<Counter<Count>> = Vec::with_capacity(4);
45     let c1 = Counter::new(Count::One);
46     c.push(c1);
47     let c2 = Counter::new(Count::Two(2));
48     c.push(c2);
49     let c3 = Counter::new(Count::Three("three".to_string()));
50     c.push(c3);
51     let c4 = Counter::new(Count::Four { id: 4, name: "four".to_string() });
52     c.push(c4);
53 
54     let r: Vec<String> = c.into_iter().map(|i| i.item.show()).collect();
55 
56     for s in r.into_iter() {
57         println!("-> {}", s);
58     }
59 }
60