-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Provide more explanation for Deref in String docs #43721
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
Changes from 2 commits
b298a58
2a62b91
40f5b30
fac6ce7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,7 +144,7 @@ use boxed::Box; | |
/// # Deref | ||
/// | ||
/// `String`s implement [`Deref`]`<Target=str>`, and so inherit all of [`str`]'s | ||
/// methods. In addition, this means that you can pass a `String` to any | ||
/// methods. In addition, this means that you can pass a `String` to a | ||
/// function which takes a [`&str`] by using an ampersand (`&`): | ||
/// | ||
/// ``` | ||
|
@@ -160,8 +160,33 @@ use boxed::Box; | |
/// | ||
/// This will create a [`&str`] from the `String` and pass it in. This | ||
/// conversion is very inexpensive, and so generally, functions will accept | ||
/// [`&str`]s as arguments unless they need a `String` for some specific reason. | ||
/// [`&str`]s as arguments unless they need a `String` for some specific | ||
/// reason. | ||
/// | ||
/// In certain cases Rust doesn't have enough information to make this | ||
/// conversion, known as deref coercion. In the following example a string | ||
/// slice `&'a str` implements the trait `TraitExample`, and the function | ||
/// `example_func` takes anything that implements the trait. In this case Rust | ||
/// would need to make two implicit conversions, which Rust doesn't have the | ||
/// means to do. For that reason, the following example will not compile. | ||
/// | ||
/// ```compile_fail,E0277 | ||
/// trait TraitExample {} | ||
/// | ||
/// impl<'a> TraitExample for &'a str {} | ||
/// | ||
/// fn example_func<A: TraitExample>(example_arg: A) {} | ||
/// | ||
/// fn main() { | ||
/// let example_string = String::from("example_string"); | ||
/// example_func(&example_string); | ||
/// } | ||
/// ``` | ||
/// | ||
/// What would work in this case is changing the line | ||
/// `example_func(&example_string);` to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line has a trailing whitespace making CI to reject the commit.
|
||
/// `example_func(example_string.to_str());`. This works because we're doing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the idiomatic solution is call it like this: example_func(&*example_string); You dereference a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, I'll make the change. I like the idea of having both options in there, thanks! |
||
/// the conversion explicitly, rather than relying on the implicit conversion. | ||
/// | ||
/// # Representation | ||
/// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be
since it's the name of a trait