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

Using slice notation instead of as_ref #153

Merged
merged 1 commit into from
May 25, 2018
Merged

Conversation

tathanhdinh
Copy link
Contributor

That would fix the type annotation error discussed in #148

@@ -47,7 +47,7 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
}

num_chars += new_chars;
print!("{}", Green.paint(word.as_ref()));
print!("{}", Green.paint(&word[..]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was word as &str before. is that better or worse?

Copy link
Contributor Author

@tathanhdinh tathanhdinh May 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frankly, I don't know, maybe it is a matter of taste.

Using slice notation is direct for me since I read about it in the Book; but I was not aware that &String can be casted to &str by as casting. Looking at Rustonomicon, I guess that this falls into the first case of cast (i.e. *T as *U where T, U: Sized), but I still don't know the underlying mechanism of this cast.

Edit: I think word as &str is type coercing using std::ops::Deref trait for String because the document says:

If T implements Deref<Target = U>..., then: ...Values of type &T are coerced to values of type &U

I would be happy with word as &str as well (which compile also for both stable and nightly).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for investigating. I think the slice notation is perfectly fine here.

@sharkdp sharkdp merged commit a13eb60 into sharkdp:master May 25, 2018
@sharkdp
Copy link
Owner

sharkdp commented May 25, 2018

Thank you!

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.

3 participants