Debian Codegraph @

demo/kitchen-sink/docs/rust.rs

raw
1 use core::rand::RngUtil;
2
3 fn main() {
4 for ["Alice", "Bob", "Carol"].each |&name| {
5 do spawn {
6 let v = rand::Rng().shuffle([1, 2, 3]);
7 for v.each |&num| {
8 print(fmt!("%s says: '%d'\n", name, num + 1))
9 }
10 }
11 }
12 }
13
14 fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {
15 let mut accumulator = ~[];
16 for vec::each(vector) |element| {
17 accumulator.push(function(element));
18 }
19 return accumulator;
20 }
21
22 struct ConstGenericStruct<const N: usize>([(); N]);
23 // T constrains by being an argument to GenericTrait.
24 impl<T> GenericTrait<T> for i32 { /* ... */ }
25
26 // T constrains by being an arguement to GenericStruct
27 impl<T> Trait for GenericStruct<T> { /* ... */ }
28
29 // Likewise, N constrains by being an argument to ConstGenericStruct
30 impl<const N: usize> Trait for ConstGenericStruct<N> { /* ... */ }
31
32 // T constrains by being in an associated type in a bound for type `U` which is
33 // itself a generic parameter constraining the trait.
34 impl<T, U> GenericTrait<U> for u32 where U: HasAssocType<Ty = T> { /* ... */ }
35
36 // Like previous, except the type is `(U, isize)`. `U` appears inside the type
37 // that includes `T`, and is not the type itself.
38 impl<T, U> GenericStruct<U> where (U, isize): HasAssocType<Ty = T> { /* ... */ }
39
40 //! - Inner line doc
41 //!! - Still an inner line doc (but with a bang at the beginning)
42
43 /*! - Inner block doc */
44 /*!! - Still an inner block doc (but with a bang at the beginning) */
45
46 /** - Outer block doc (exactly) 2 asterisks */
47 let multiline = r##"
48 This is a raw string.
49 r#"nested string"#
50 "##;
51 let empty = br##""##;
52 let _strings = [r"hello\", br"hello\", cr"hello\" ];
53
54 macro_rules! mac_variant {
55 ($vis:vis $name:ident) => {
56 enum $name {
57 $vis Unit,
58
59 $vis Tuple(u8, u16),
60
61 $vis Struct { f: u8 },
62 }
63 }
64 }