Rust: Tracking issue for vNext edition prelude

Created on 17 Oct 2019  Â·  18Comments  Â·  Source: rust-lang/rust

There were plans to do this for the 2018 edition, but it never happened (https://github.com/rust-lang/rust/issues/51418, https://github.com/rust-lang/rust/pull/51434)

Things to consider:

  • [ ] TryFrom/TryInto
  • [ ] ???

Note 2020-04-19: Please limit suggestions here to things that would be breaking changes to do in the current prelude. Some prelude additions can happen without being a breaking change, and if you'd like to do one of those them just talk with libs about it -- it doesn't need to wait.

C-tracking-issue E-help-wanted Libs-Tracked T-lang T-libs lang-team-202x-edition

Most helpful comment

I use drop more often than try_from, for example. Probably because one is in the prelude, and the other isn't :)

I think removal of drop is an unnecessary churn:

  • it has legitimate use-cases, like unlocking mutexes early, explicitly disconnecting mpsc senders, removing ballast from Futures.
  • it's been used in code examples and the Rust Book.
  • it's harmless, and it's not deprecated.
  • dropping it would not improve anyone's Rust experience. There's no other drop function waiting to be used instead.

All 18 comments

std::fmt::Display seems like a reasonable thing to include IMO.

@jhpratt

  • Why should it be added?

  • Why this one and not other items from std::fmt? Display is rarely called directly but more often implemented. But to implement it you also need std::fmt::Formatter and <std::fmt::Result or std::fmt::Error>. But we already have std::result::Result in the prelude, and std::io::Error and std::error::Error that are not but share the exact same name and don’t seem to me less important than std::fmt::Error.

  • If it’s added, why in an edition-specific prelude rather than for all editions? Edition-specific prelude are only discussed because adding TryFrom makes many existing calls to try_from methods of other traits ambiguous, breaking code that runs on Stable: https://github.com/rust-lang/rust/pull/49305#issuecomment-376759728

Can I also suggest an ArrayIntoIterExt extension trait for the 2021 prelude?

AFAIK implementing IntoIterator for arrays is blocked on clashes with existing ecosystem implementations. This could be worked around starting with the 2021 edition by creating an extension trait for arrays which implements IntoIterator, and exposing it in the 2021 prelude.

A similar approach is currently to expose slice concatenation methods for technical reasons I still don't quite understand. So this approach, while unusual, is not completely unheard of.

In addition to TryInto, I find adding quite frequently:

  • Path and PathBuf
  • HashMap and HashSet
  • Arc and Mutex

I wouldn't add Display, because for implementing it, it also needs its formatter and result. It would be interesting to add std::fmt to the prelude, so that fmt::Formatter and fmt::Result work, but adding use std::fmt isn't too bad.

If it’s added, why in an edition-specific prelude rather than for all editions? Edition-specific prelude are only discussed because adding TryFrom makes many existing calls to try_from methods of other traits ambiguous, breaking code that runs on Stable: #49305 (comment)

@eaglgenes101 The problem with https://github.com/rust-lang/rust/issues/25725 is not collision with methods of other traits from other crates, but collision of IntoInterator for [T; N] with itself for &[T; N] though auto-ref. Code that was previously valid (iterating &T by reference) changes meaning.

Having a new dedicated trait does not change the problem if the method is still named into_iter. And if it’s named something else, we’re back to the previous point that it doesn’t need to be edition-specific.

I have previously suggested adding FromIterator to the prelude. Rationale here.

I would love to see the following in an updated prelude:

  • Error
  • TryInfo and TryFrom
  • Path and PathBuf
  • Cow
  • HashMap, HashSet, BTreeMap, BTreeSet
  • File

I've based these on things I see imported heavily, and some greps for commonly imported bits of std in various projects.

I'm also tempted to request OsStr and OsString.

@mbrubeck @joshtriplett Aside for TryInto and TryFrom (where we already know this is the case), do you expect that adding those to std::prelude::v1 for all editions would cause any breakage? If not, we shouldn’t make things edition-specific gratuitously and this is not the right thread to request additions.

structopt, kuchiki, micromath, and clapme have trait methods that could conflict with FromIterator::from_iter.

Of these, clapme (which is not very widely used currently) definitely has conflicts: clapme::ClapMe is implemented for Vec and Option, which also implement FromIterator. The others could conflict in theory because of third-party impls, but I think such conflicts are unlikely in practice.

My best guess is that adding FromIterator to the prelude for all editions would break some existing code, but not a huge amount.

Here's one that may be slightly controversial — remove std::mem::drop from the prelude. I can't think of a single instance I've ever used it, and IMO having to explicitly import it would be clearer.

Here's one that may be slightly controversial — remove std::mem::drop from the prelude. I can't think of a single instance I've ever used it, and IMO having to explicitly import it would be clearer.

+1

I've been writing Rust for around 2 years now, and I never realized until now that drop was in the prelude -- I've always imported std::mem and used it as mem::drop(x). I don't recall ever seeing any code that uses it from the prelude.

I'm regularly using drop from the prelude. It's very handy in async/await code where it's efficient and sometimes required to explicitly drop stuff before .await points.

@kornelski Is it something that most people use on a regular basis though? Other items in the prelude are used _all the time by a lot of people_.

I use drop more often than try_from, for example. Probably because one is in the prelude, and the other isn't :)

I think removal of drop is an unnecessary churn:

  • it has legitimate use-cases, like unlocking mutexes early, explicitly disconnecting mpsc senders, removing ballast from Futures.
  • it's been used in code examples and the Rust Book.
  • it's harmless, and it's not deprecated.
  • dropping it would not improve anyone's Rust experience. There's no other drop function waiting to be used instead.

Error I'm sure breaks someone.

HashMap and HashSet probably break people as well.

Error I'm sure breaks someone. HashMap and HashSet probably break people as well.

As far as my understanding goes, adding types (as opposed to traits) to the prelude is not a breaking change. Prelude is shadowed by glob and explicit imports.

We talked about this in the lang team triage meeting today, in the context of https://github.com/rust-lang/lang-team/issues/44.

The zulip thread (https://rust-lang.zulipchat.com/#narrow/stream/243200-t-lang.2Fmajor-changes/topic/MCP.3A.20per-edition.20preludes.20lang-team.2344/near/205701158) raised a question about how this is going to interact with name resolution in macros. We'd appreciate it if someone in the community could take a deep dive into the best way to resolve this specific question, as overall we'd probably like to do this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

withoutboats picture withoutboats  Â·  211Comments

Leo1003 picture Leo1003  Â·  898Comments

withoutboats picture withoutboats  Â·  308Comments

nikomatsakis picture nikomatsakis  Â·  268Comments

nikomatsakis picture nikomatsakis  Â·  236Comments