0

I'm learning rust for fun after C++ class and I'm wondering, can use std::io break my code after update of rustc edition to newer?

For example, in C++ using using namespace std is bad, because if new function gets added to std your multiple translation units code may break after compiler update because of function with name same as function written by you was added to namespace std.

However in all the official rust tutorials use std::io is used.

Can use std::io break my code in rust?

blonded04
  • 147
  • 9
  • 2
    ?? that doesn't really make sense, the rust imports still have to be namespace qualified unless you import specific identifiers. I haven't used C++ in about a decade but IIRC `using namespace std;` is bad because imports all the symbols into your current scope without qualification. N.B. you can use a star import in Rust, which would suffer from the same issue potentially although nothing in Rust is as massive as `std` in C++. – Jared Smith Jan 02 '22 at 18:45
  • @JaredSmith okay, but if we need to create crate called `io` (for example we are writing some sort of parser/whatever) and put some functions in it it might break? – blonded04 Jan 02 '22 at 18:48
  • Firstly, just no. Do not name crates after the modules in the standard library. Secondly, just don't ever use star imports and you'll be fine. – Jared Smith Jan 02 '22 at 18:50
  • So convention is just not to name crate after parts of `std` and/or not using star imports? – blonded04 Jan 02 '22 at 18:52
  • General guidelines for rust imports can be found in the [Rust style guide](https://doc.rust-lang.org/1.0.0/style/style/imports.html). There are mentions of `extern crate` in there but nowadays those are automatically added to your source code by `cargo` for your declared dependencies. For this exact reason, it is not recommended to use `*` imports so that new functions/types do not lead to any conflicts. – Kendas Jan 02 '22 at 19:11
  • 1
    @blonded04 there's no reason that you couldn't name a crate `io`, any difficulty derived from that could be caused between any two crates/modules and I've never encountered problems like that in practice. You can always disambiguate the crate with `::io` if there were actually a problem. – kmdreko Jan 02 '22 at 19:18

1 Answers1

4

Just use std::io; on its own cannot break your code between versions. That declaration only brings the io name into scope and that won't change.

If you had done use std::io::*;, that would bring everything from the io module into scope similar to use namespace std; in C++ and thus could break your code in the future, but wildcard imports are discouraged in general for that reason.

kmdreko
  • 42,554
  • 6
  • 57
  • 106