Skip to main content
Shrink the immutable map before returning it.
Source Link
Davislor
  • 9.2k
  • 19
  • 39
use std::collections::HashMap;

struct Command {
    code: char,
    description: &'static str,
}

fn translation_build() -> HashMap<&'static str, String> {
    let mut map = HashMap::new();

    for (key, value) in [
        ("START", "Start your instance"),
        ("STOP", "Stop your instance"),
        ("QUIT", "Quit"),
        ("STARTING", "Starting instance"),
        ("STOPPING", "Stopping instance"),
        ("QUITTING", "Quitting"),
        ("UNKNOWN_CMD", "Invalid option"),
    ] {
        map.insert(key, value.to_string());
    }
    map.shrink_to_fit();
    map
}

fn translation_lookup(key: &str) -> Option<&'static str> {
    use once_cell::sync::Lazy;
    static STRING_TABLE: Lazy<HashMap<&'static str, String>> = Lazy::new(translation_build);

    (&*STRING_TABLE).get(key).map(String::as_str)
}

fn main() {
    let commands = [
        Command {
            code: '1',
            description: translation_lookup("START").unwrap_or("START"),
        },
        Command {
            code: '2',
            description: translation_lookup("STOP").unwrap_or("STOP"),
        },
        Command {
            code: '3',
            description: translation_lookup("QUIT").unwrap_or("QUIT"),
        },
    ];

    for c in commands {
        println!(" {}: {}", c.code, c.description);
    }
}
use std::collections::HashMap;

struct Command {
    code: char,
    description: &'static str,
}

fn translation_build() -> HashMap<&'static str, String> {
    let mut map = HashMap::new();

    for (key, value) in [
        ("START", "Start your instance"),
        ("STOP", "Stop your instance"),
        ("QUIT", "Quit"),
        ("STARTING", "Starting instance"),
        ("STOPPING", "Stopping instance"),
        ("QUITTING", "Quitting"),
        ("UNKNOWN_CMD", "Invalid option"),
    ] {
        map.insert(key, value.to_string());
    }

    map
}

fn translation_lookup(key: &str) -> Option<&'static str> {
    use once_cell::sync::Lazy;
    static STRING_TABLE: Lazy<HashMap<&'static str, String>> = Lazy::new(translation_build);

    (&*STRING_TABLE).get(key).map(String::as_str)
}

fn main() {
    let commands = [
        Command {
            code: '1',
            description: translation_lookup("START").unwrap_or("START"),
        },
        Command {
            code: '2',
            description: translation_lookup("STOP").unwrap_or("STOP"),
        },
        Command {
            code: '3',
            description: translation_lookup("QUIT").unwrap_or("QUIT"),
        },
    ];

    for c in commands {
        println!(" {}: {}", c.code, c.description);
    }
}
use std::collections::HashMap;

struct Command {
    code: char,
    description: &'static str,
}

fn translation_build() -> HashMap<&'static str, String> {
    let mut map = HashMap::new();

    for (key, value) in [
        ("START", "Start your instance"),
        ("STOP", "Stop your instance"),
        ("QUIT", "Quit"),
        ("STARTING", "Starting instance"),
        ("STOPPING", "Stopping instance"),
        ("QUITTING", "Quitting"),
        ("UNKNOWN_CMD", "Invalid option"),
    ] {
        map.insert(key, value.to_string());
    }
    map.shrink_to_fit();
    map
}

fn translation_lookup(key: &str) -> Option<&'static str> {
    use once_cell::sync::Lazy;
    static STRING_TABLE: Lazy<HashMap<&'static str, String>> = Lazy::new(translation_build);

    (&*STRING_TABLE).get(key).map(String::as_str)
}

fn main() {
    let commands = [
        Command {
            code: '1',
            description: translation_lookup("START").unwrap_or("START"),
        },
        Command {
            code: '2',
            description: translation_lookup("STOP").unwrap_or("STOP"),
        },
        Command {
            code: '3',
            description: translation_lookup("QUIT").unwrap_or("QUIT"),
        },
    ];

    for c in commands {
        println!(" {}: {}", c.code, c.description);
    }
}
added 184 characters in body
Source Link
Davislor
  • 9.2k
  • 19
  • 39

This is only slightly more complicated than the classic sunsum:

.fold(0, |a|accumulator, b|current| a+baccumulator+current)
.fold(1, |a|accumulator, b|current| a*baccumulator*current)

This is only slightly more complicated than the classic sun:

.fold(0, |a, b| a+b)
.fold(1, |a, b| a*b)

This is only slightly more complicated than the classic sum:

.fold(0, |accumulator, current| accumulator+current)
.fold(1, |accumulator, current| accumulator*current)
added 184 characters in body
Source Link
Davislor
  • 9.2k
  • 19
  • 39

Or you might prefer to express this more concisely, as an accumulating fold:

    let command_width = commands
        .iter()
        .fold(0, |x, c| max(x, c.description.len()));

This is only slightly more complicated than the classic sun:

.fold(0, |a, b| a+b)

or product:

.fold(1, |a, b| a*b)

You could also map-reduce a pair of values in a single pass, using a closure.

You could also map-reduce a pair of values in a single pass, using a closure.

Or you might prefer to express this more concisely, as an accumulating fold:

    let command_width = commands
        .iter()
        .fold(0, |x, c| max(x, c.description.len()));

This is only slightly more complicated than the classic sun:

.fold(0, |a, b| a+b)

or product:

.fold(1, |a, b| a*b)

You could also map-reduce a pair of values in a single pass, using a closure.

Source Link
Davislor
  • 9.2k
  • 19
  • 39
Loading