Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
acjay opened this issue May 9, 2024 · 4 comments
Open

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

acjay opened this issue May 9, 2024 · 4 comments

Comments

@acjay
Copy link

acjay commented May 9, 2024

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.

@vitiral
Copy link
Owner

vitiral commented May 9, 2024 via email

@vitiral
Copy link
Owner

vitiral commented May 9, 2024

if you have static strings, then just do format!("x = {}, y = {val}", 10, val = 30);

@vitiral vitiral closed this as completed May 9, 2024
@acjay
Copy link
Author

acjay commented May 9, 2024

That only works if you have a literal string for the format. I didn't demonstrate this in my example, but the reason I'm trying to do this is because I want to have the format string reside in a constant in a different module.

@vitiral
Copy link
Owner

vitiral commented May 10, 2024

Yes, that use-case isn't supported by this library.

In theory I suppose it could support AsRef<str> keys and values. I'd be willing to accept a PR which did that.

@vitiral vitiral reopened this May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants