Open obsgolem opened 3 months ago
From The Rust Programming Language section 7.4:
Creating Idiomatic
use
PathsAlthough both Listing 7-11 and 7-13 accomplish the same task, Listing 7-11 is the idiomatic way to bring a function into scope with use. Bringing the function’s parent module into scope with use means we have to specify the parent module when calling the function. Specifying the parent module when calling the function makes it clear that the function isn’t locally defined while still minimizing repetition of the full path.
We have been encouraged for a long time to always import the parent module when calling the function to distinguish locally defined functions and imported functions. However, the syntax sugar suggested in this RFC contradicts to this convention.
I think it is more appropriate to discuss this convention in the RFC to make it more clear how we should use this feature. :)
I'd like to reiterate here what I said on #1995
Many libraries define free functions for constructors to mathy types, like vectors.
Some examples: https://docs.rs/glam/0.24.2/src/glam/f32/vec2.rs.html#12 https://docs.rs/egui/latest/egui/fn.pos2.html
It would be nice if instead of having to create wrapper functions, the user (or the lib author with pub use
) could just do use Vec2::new as vec2;
This is some additional motivation that could go into the motivation section. Default::default
is hardly the only use case for this.
That being said, I noticed that this RFC doesn't talk about importing inherent methods, which I think should be addressed. If this RFC only proposes importing trait methods, but not inherent methods, then the use cases I mentioned above are nullified. Why should we only support importing trait methods, but not inherent methods?
I discussed that in the future work section. use Type::method
is out of scope for this RFC. The difficulty is impl
blocks with arbitrary where clauses.
I'm going to go ahead and start the process of seeing if we have consensus on this:
@rfcbot merge
Please do either add support for associated constants inline in the RFC (if it's straightforward to do so) or mention them in future work (if not):
@rfcbot concern associated-constants
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:
Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!
cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. See this document for info about what commands tagged team members can give me.
Apologies for the delay, wasn't able to work up motivation to work on this again until today. Thanks for the review!
👍 to the motivation here.
@rfcbot concern what-about-turbofish
One thing I think needs to be addressed in the reference section is what's supposed to happen with generic parameters.
If one has
trait Trait<A> {
fn method<B>(…);
}
and does a
use othermod::Trait::method;
What happens? Can you turbofish it? Which generics are there there?
The precedent from enum variants doesn't cover generics on the associated items, so something needs to specified for it.
This can be inferred directly from the desugaring described in the RFC. If the trait has generics, they must be inferrable in order for Trait::method()
to work, hence the same is true for the imported version. I can spell that out explicitly if you want.
Rendered
This feature fully supplants https://github.com/rust-lang/rust/pull/73001, allowing you to do things like:
and more.
This is my first RFC, please forgive any missteps I make in the process.
Partially completes #1995.