-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for RFC 2306, "Add core::convert::identity" #53500
Comments
Add the identity function as core::convert::identity ## New notes This implements rust-lang/rfcs#2306 (see #53500). ## Old notes (ignore this in new reviews) Adds the identity function `fn id<T>(x: T) -> T { x }` to core::convert and the prelude. Some motivations for why this is useful are explained in the doc tests. Another is that using the identity function instead of `{ x }` or `|x| x` makes it clear that you intended to use an identity conversion on purpose. The reasoning: + behind adding this to `convert` and not `mem` is that this is an identity *conversion*. + for adding this to the prelude is that it should be easy enough to use that the ease of writing your own identity function or using a closure `|x| x` doesn't overtake that. I've separated this out into two feature gates so that the addition to the prelude can be considered and stabilized separately. cc @bluss
The documentation suggests: #![feature(convert_id)]
use std::convert::identity;
let iter = vec![Some(1), None, Some(3)].into_iter();
let filtered = iter.filter_map(identity).collect::<Vec<_>>();
assert_eq!(vec![1, 3], filtered); I'd argue this is actively non-idiomatic Rust. It is better to use let iter = vec![Some(1), None, Some(3)].into_iter();
let filtered = iter.flatten().collect::<Vec<_>>();
assert_eq!(vec![1, 3], filtered); I'm bringing this up here because if we can't have good examples, perhaps the feature doesn't carry it's own weight. |
The documentation suggests using #![feature(convert_id)]
use std::convert::identity;
fn manipulation(x: u32, y: u32) -> u32 {
x + y
}
fn main() {
let do_stuff = if true { manipulation } else { identity };
let _results = do_stuff(1, 2);
}
|
So However, I think that: let filtered = iter.filter_map(identity).collect::<Vec<_>>(); is more clear with respect to intent. To know that
Given that Rust is a typed language I am unsure as to why one would expect this to work with functions of different arity. This would be the same as expecting |
@SimonSapin How do you feel about stabilizing this? |
Let’s. @rfcbot fcp merge |
Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), 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! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
Stabilize core::convert::identity r? @SimonSapin fixes #53500 This is waiting for FCP to complete but in the interim it would be good to review.
Add the identity function as core::convert::identity ## New notes This implements rust-lang/rfcs#2306 (see rust-lang/rust#53500). ## Old notes (ignore this in new reviews) Adds the identity function `fn id<T>(x: T) -> T { x }` to core::convert and the prelude. Some motivations for why this is useful are explained in the doc tests. Another is that using the identity function instead of `{ x }` or `|x| x` makes it clear that you intended to use an identity conversion on purpose. The reasoning: + behind adding this to `convert` and not `mem` is that this is an identity *conversion*. + for adding this to the prelude is that it should be easy enough to use that the ease of writing your own identity function or using a closure `|x| x` doesn't overtake that. I've separated this out into two feature gates so that the addition to the prelude can be considered and stabilized separately. cc @bluss
This is a tracking issue for the RFC "Add
fn identity<T>(x: T) -> T { x }
tocore::convert
" (rust-lang/rfcs#2306).Steps:
Unresolved questions:
None.
The text was updated successfully, but these errors were encountered: