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

fix for a couple of small issues I came accross whilst doing the exercises #2159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

markharley12
Copy link

@markharley12 markharley12 commented Nov 15, 2024

Issue 1:
The error you're encountering is due to the use of HashMap::from_iter, which is not a method directly associated with HashMap. Instead, from_iter is a method provided by the FromIterator trait. To fix this issue, you have two main options:

Option 1: Import the FromIterator trait

You can import the FromIterator trait into your scope, which will allow you to use HashMap::from_iter. Add the following line at the top of your file:

Option 2: Use HashMap::from Instead

A more idiomatic way is to use HashMap::from, which can directly convert an array of key-value pairs into a HashMap. Replace HashMap::from_iter(content) with HashMap::from(content).

Explanation:

Using HashMap::from: This function is designed to create a HashMap from an array of key-value pairs, which is exactly what content is.
Using FromIterator Trait: If you prefer to use from_iter, importing the FromIterator trait brings the method into scope.
Recommendation:

I recommend using Option 2 (HashMap::from) because it's more straightforward and doesn't require importing additional traits. It's also more idiomatic for converting arrays into collections in Rust.

Issue 2:

println! Supports Named Arguments (Rust 1.58+):

If you use println! with named arguments like {name:?}, it works as expected if you provide the arguments or capture them from scope.
panic! Does Not Support Named Arguments:

Always use positional arguments with panic!. Named arguments (e.g., name = value) will cause warnings or runtime issues.
Warnings About Placeholders in panic!:

If the format string in panic! has placeholders but no arguments are provided, the placeholder is treated as a literal. This behavior produces a warning in Rust 2021.

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

Successfully merging this pull request may close these issues.

1 participant