summaryrefslogtreecommitdiff
path: root/common/templates/rust/src/lib.rs
blob: ee5d017d7d95f657a54d31c933def4cb377d4964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::any::type_name;

pub fn hello_world(action: &str) {
    // Every function in Rust has a unique, unnameable type.
    // we define a tiny helper to extract that type's name.
    fn get_type_name<T>(_: T) -> &'static str {
        type_name::<T>()
    }

    // This gives us "current_crate::hello_world"
    let full_path = get_type_name(hello_world);

    // We transform the string entirely through a lazy iterator pipeline.
    let formatted: String = full_path
        .split("::")
        .last()
        .unwrap_or("")
        .split('_')
        .map(|word| {
            // Capitalize the first letter and chain the rest
            let mut chars = word.chars();
            chars
                .next()
                .map(|f| f.to_uppercase().collect::<String>() + chars.as_str())
                .unwrap_or_default()
        })
        .collect::<Vec<_>>()
        .join(", ")
        + "!";

    println!("{}", formatted)
}