Skip to content

strfmt doesn't work with literal strings as keys #38

@acjay

Description

@acjay

I might be doing something wrong, but it appears that strfmt doesn't accept a HashMap with keys that are string literals (i.e. &'static str). This seems weird to me, as this seems like a common situation.

I would naively expect the following to work:

use std::collections::HashMap;
use strfmt::strfmt;

fn main() {
    let mut params: HashMap<&str, &str> = HashMap::new();
    params.insert("word1", "hello");
    params.insert("word2", "world");

    println!("{}", strfmt("{word1} {word2}", &params).unwrap());
}

But it fails to compile, with the following error:

error[E0277]: the trait bound `&str: FromStr` is not satisfied
  --> src/main.rs:9:46
   |
9  |     println!("{}", strfmt("{word1} {word2}", &params).unwrap());
   |                    ------                    ^^^^^^^ the trait `FromStr` is not implemented for `&str`
   |                    |
   |                    required by a bound introduced by this call
   |
   = help: the trait `FromStr` is implemented for `String`
note: required by a bound in `strfmt`
  --> /Users/alanjohnson/.cargo/registry/src/index.crates.io-6f17d22bba15001f/strfmt-0.2.4/src/lib.rs:55:20
   |
53 | pub fn strfmt<'a, K, T: DisplayStr>(fmtstr: &str, vars: &HashMap<K, T>) -> Result<String>
   |        ------ required by a bound in this function
54 | where
55 |     K: Hash + Eq + FromStr,
   |                    ^^^^^^^ required by this bound in `strfmt`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `strfmt_test` (bin "strfmt_test") due to 1 previous error

One fix is to explicit convert the string literals to String:

use std::collections::HashMap;
use strfmt::strfmt;

fn main() {
    let mut params: HashMap<String, &str> = HashMap::new();
    params.insert("word1".to_string(), "hello");
    params.insert("word2".to_string(), "world");

    println!("{}", strfmt("{word1} {word2}", &params).unwrap());
}

This seems kinda weird to me, because I would think this is a common use case and that there wouldn't be a need to add the boilerplate of string conversions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions