Skip to main content
deleted 218 characters in body
Source Link
Ibraheem Ahmed
  • 12.9k
  • 2
  • 52
  • 59

No more extern crateextern crate

You no longer need to write extern crateextern crate to import a crate into your project. Before:

// Rust 2015

extern crate futures;

mod submodulefoo {
    use futures::Future;
}

// Rust 2018

mod submodulefoo {
    use futures::Future;
}

One other use for extern crate was to import macros; that's no longer needed. In your Rust 2015, you would have written:

// Rust 2015

#[macro_use]
extern crate bar;log;

fn main() {
    bazerror!("oops");
}
// Rust 2018

use barlog::baz;error;

fn main() {
    bazerror!("oops");
}
extern crate futures as f;fut;
use futures as f;fut;

use self::ffut::Future;

There's one exception to this rule:Sysroot Crates

There's one exception to this rule, and that's the "sysroot" crates. These are the crates distributed with Rust itself. We'd eventually like to remove the requirement for extern crate for them as wellFor now, but it hasn't shipped yet.

You'llyou still need to use extern crateextern crate for these crates:

  • proc_macro

Additionally, you would need to use it for:

  • core
  • std

However, extern crate std;std is already implicit, and with #![no_std], extern crate core; is are already implicit. You'll only, so it is very rare that you will need these in highly specialized situationsto declare them manually.

// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

If there's no edition`edition key, Cargo will default to Rust 2015. But in this case, we've chosen 2018, and so our code is compiling with Rust 2018! Thanks to @KevinReid for pointing this out

No more extern crate

You no longer need to write extern crate to import a crate into your project. Before:

// Rust 2015

extern crate futures;

mod submodule {
    use futures::Future;
}

// Rust 2018

mod submodule {
    use futures::Future;
}

One other use for extern crate was to import macros; that's no longer needed. In your Rust 2015, you would have written:

// Rust 2015

#[macro_use]
extern crate bar;

fn main() {
    baz!();
}
// Rust 2018

use bar::baz;

fn main() {
    baz!();
}
extern crate futures as f;
use futures as f;

use self::f::Future;

There's one exception to this rule:

There's one exception to this rule, and that's the "sysroot" crates. These are the crates distributed with Rust itself. We'd eventually like to remove the requirement for extern crate for them as well, but it hasn't shipped yet.

You'll need to use extern crate for:

  • proc_macro

Additionally, you would need to use it for:

  • core
  • std

However, extern crate std; is already implicit, and with #![no_std], extern crate core; is already implicit. You'll only need these in highly specialized situations.

// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

If there's no edition` key, Cargo will default to Rust 2015. But in this case, we've chosen 2018, and so our code is compiling with Rust 2018! Thanks to @KevinReid for pointing this out

No more extern crate

You no longer need to write extern crate to import a crate into your project. Before:

// Rust 2015

extern crate futures;

mod foo {
    use futures::Future;
}

// Rust 2018

mod foo {
    use futures::Future;
}

One other use for extern crate was to import macros; that's no longer needed. In Rust 2015, you would have written:

// Rust 2015

#[macro_use]
extern crate log;

fn main() {
    error!("oops");
}
// Rust 2018

use log::error;

fn main() {
    error!("oops");
}
extern crate futures as fut;
use futures as fut;

use fut::Future;

Sysroot Crates

There's one exception to this rule, and that's the "sysroot" crates. These are the crates distributed with Rust itself. For now, you still need to use extern crate for these crates:

  • proc_macro
  • core
  • std

However, extern crate std and extern crate core are already implicit, so it is very rare that you will need to declare them manually.

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

If there's no edition key, Cargo will default to Rust 2015. But in this case, we've chosen 2018, and so our code is compiling with Rust 2018! Thanks to @KevinReid for pointing this out

deleted 1 character in body
Source Link
Ibraheem Ahmed
  • 12.9k
  • 2
  • 52
  • 59

Tl;dr: You do not need to write extern crate anymore for external dependencies in Rust 2018. The code you provided without extern crate works just fine with edition = "2018" set in your Cargo.tomltldr: You do not need to write extern crate anymore for external dependencies in Rust 2018. The code you provided without extern crate works just fine with edition = "2018" set in your Cargo.toml

Tl;dr: You do not need to write extern crate anymore for external dependencies in Rust 2018. The code you provided without extern crate works just fine with edition = "2018" set in your Cargo.toml

tldr: You do not need to write extern crate anymore for external dependencies in Rust 2018. The code you provided without extern crate works just fine with edition = "2018" set in your Cargo.toml

added 493 characters in body
Source Link
Ibraheem Ahmed
  • 12.9k
  • 2
  • 52
  • 59

Tl;dr: You do not need to write extern crate anymore for external dependencies in Rust 2018. The code you provided without extern crate works just fine with edition = "2018" set in your Cargo.toml

No more extern crate

You no longer need to write extern crate to import a crate into your project. Before:

// Rust 2015

extern crate futures;

mod submodule {
    use futures::Future;
}

After:

// Rust 2018

mod submodule {
    use futures::Future;
}

Macros

One other use for extern crate was to import macros; that's no longer needed. In your Rust 2015, you would have written:

// Rust 2015

#[macro_use]
extern crate bar;

fn main() {
    baz!();
}

Now, you write:

// Rust 2018

use bar::baz;

fn main() {
    baz!();
}

Renaming crates

If you've been using as to rename your crate like this:

extern crate futures as f;

Then in Rust 2018, you simply do this:

use futures as f;

use self::f::Future;

There's one exception to this rule:

There's one exception to this rule, and that's the "sysroot" crates. These are the crates distributed with Rust itself. We'd eventually like to remove the requirement for extern crate for them as well, but it hasn't shipped yet.

You'll need to use extern crate for:

  • proc_macro

Additionally, you would need to use it for:

  • core
  • std

However, extern crate std; is already implicit, and with #![no_std], extern crate core; is already implicit. You'll only need these in highly specialized situations.

Finally, on nightly, you'll need it for crates like:

  • alloc
  • test

Those are the only exceptions to the rule. Therefore, the code you provided without extern crate works just fine in Rust 2018:

// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

Setting your Rust Edition

Just because you have the latest Rust version installed does not mean that you are compiling with the latest edition. To tell Cargo to use a specific edition, set the edition key/value pair. For example:

[package]
name = "foo"
edition = "2018"

If there's no edition` key, Cargo will default to Rust 2015. But in this case, we've chosen 2018, and so our code is compiling with Rust 2018! Thanks to @KevinReid for pointing this out

This answer was derived from The Rust Edition Guide

Tl;dr: You do not need to write extern crate anymore for external dependencies in Rust 2018. The code you provided without extern crate works just fine

No more extern crate

You no longer need to write extern crate to import a crate into your project. Before:

// Rust 2015

extern crate futures;

mod submodule {
    use futures::Future;
}

After:

// Rust 2018

mod submodule {
    use futures::Future;
}

Macros

One other use for extern crate was to import macros; that's no longer needed. In your Rust 2015, you would have written:

// Rust 2015

#[macro_use]
extern crate bar;

fn main() {
    baz!();
}

Now, you write:

// Rust 2018

use bar::baz;

fn main() {
    baz!();
}

Renaming crates

If you've been using as to rename your crate like this:

extern crate futures as f;

Then in Rust 2018, you simply do this:

use futures as f;

use self::f::Future;

There's one exception to this rule:

There's one exception to this rule, and that's the "sysroot" crates. These are the crates distributed with Rust itself. We'd eventually like to remove the requirement for extern crate for them as well, but it hasn't shipped yet.

You'll need to use extern crate for:

  • proc_macro

Additionally, you would need to use it for:

  • core
  • std

However, extern crate std; is already implicit, and with #![no_std], extern crate core; is already implicit. You'll only need these in highly specialized situations.

Finally, on nightly, you'll need it for crates like:

  • alloc
  • test

Those are the only exceptions to the rule. Therefore, the code you provided without extern crate works just fine in Rust 2018:

// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

This answer was derived from The Rust Edition Guide

Tl;dr: You do not need to write extern crate anymore for external dependencies in Rust 2018. The code you provided without extern crate works just fine with edition = "2018" set in your Cargo.toml

No more extern crate

You no longer need to write extern crate to import a crate into your project. Before:

// Rust 2015

extern crate futures;

mod submodule {
    use futures::Future;
}

After:

// Rust 2018

mod submodule {
    use futures::Future;
}

Macros

One other use for extern crate was to import macros; that's no longer needed. In your Rust 2015, you would have written:

// Rust 2015

#[macro_use]
extern crate bar;

fn main() {
    baz!();
}

Now, you write:

// Rust 2018

use bar::baz;

fn main() {
    baz!();
}

Renaming crates

If you've been using as to rename your crate like this:

extern crate futures as f;

Then in Rust 2018, you simply do this:

use futures as f;

use self::f::Future;

There's one exception to this rule:

There's one exception to this rule, and that's the "sysroot" crates. These are the crates distributed with Rust itself. We'd eventually like to remove the requirement for extern crate for them as well, but it hasn't shipped yet.

You'll need to use extern crate for:

  • proc_macro

Additionally, you would need to use it for:

  • core
  • std

However, extern crate std; is already implicit, and with #![no_std], extern crate core; is already implicit. You'll only need these in highly specialized situations.

Finally, on nightly, you'll need it for crates like:

  • alloc
  • test

Those are the only exceptions to the rule. Therefore, the code you provided without extern crate works just fine in Rust 2018:

// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

Setting your Rust Edition

Just because you have the latest Rust version installed does not mean that you are compiling with the latest edition. To tell Cargo to use a specific edition, set the edition key/value pair. For example:

[package]
name = "foo"
edition = "2018"

If there's no edition` key, Cargo will default to Rust 2015. But in this case, we've chosen 2018, and so our code is compiling with Rust 2018! Thanks to @KevinReid for pointing this out

This answer was derived from The Rust Edition Guide

added 13 characters in body
Source Link
Ibraheem Ahmed
  • 12.9k
  • 2
  • 52
  • 59
Loading
added 217 characters in body
Source Link
Ibraheem Ahmed
  • 12.9k
  • 2
  • 52
  • 59
Loading
Source Link
Ibraheem Ahmed
  • 12.9k
  • 2
  • 52
  • 59
Loading