Rust 为什么有时需要外部板条箱?
我想知道,为什么有时我们需要使用Rust 为什么有时需要外部板条箱?,rust,module,Rust,Module,我想知道,为什么有时我们需要使用外部板条箱而不是使用?我正在使用板条箱wee_alloc,要导入其模块,我必须使用外部板条箱: extern crate wee_alloc; // Use `wee_alloc` as the global allocator. #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 但是有了web\u sys我就可以使用usetldr:在2018年
外部板条箱
而不是使用
?我正在使用板条箱wee_alloc
,要导入其模块,我必须使用外部板条箱
:
extern crate wee_alloc;
// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
但是有了
web\u sys
我就可以使用use
tldr:在2018年,您不需要再为外部依赖性编写外部板条箱了。您提供的代码不带外部板条箱
与edition=“2018”
设置在您的Cargo.toml
不再使用外部板条箱
您不再需要编写外部板条箱
来将板条箱导入到项目中。之前:
//Rust 2015
外部板条箱期货;
莫德福{
使用期货:未来;
}
之后:
//2018
莫德福{
使用期货:未来;
}
宏
extern板条箱的另一个用途是导入宏;这已经不需要了。在Rust 2015中,您可能会写道:
//Rust 2015
#[宏_使用]
外部板条箱日志;
fn main(){
错误!(“oops”);
}
现在,你写:
//2018
使用日志::错误;
fn main(){
错误!(“oops”);
}
重新命名板条箱
如果您一直使用as
重命名您的板条箱,如下所示:
extern crate futures as fut;
然后在Rust 2018中,您只需执行以下操作:
use futures as fut;
use fut::Future;
系统根板条箱
这条规则有一个例外,那就是“系统根”板条箱。这些板条箱本身就有锈迹。目前,您仍然需要为这些板条箱使用外部板条箱
:
proc\u宏
core
std
但是,extern-claiter-std
和extern-claiter-core
已经是隐式的,因此很少需要手动声明它们
最后,在夜间,您需要将其用于板条箱,如:
alloc
测试
这些是该规则的唯一例外。因此,您提供的代码没有extern-crater
在Rust 2018中运行良好:
#[全局#分配器]
静态ALLOC:wee_ALLOC::WeeAlloc=wee_ALLOC::WeeAlloc::INIT;
设置生锈版本
安装了最新的Rust版本并不意味着您正在使用最新版本进行编译。要告诉Cargo使用特定版本,请设置edition
键/值对。例如:
[软件包]
name=“foo”
edition=“2018”
如果没有edition
键,Cargo将默认为Rust 2015。但在本例中,我们选择了2018年,因此我们的代码是用Rust 2018编译的!感谢@KevinReid指出这一点
这个答案来源于你不需要外部板条箱
——只有在你使用2015版时才需要。我使用的是最新版本的rust,我看到使用使用而不是外部板条箱时没有任何外部板条箱
错误。你能提供完整的错误信息吗?2021年,不需要使用这部分回答了我的问题。我仍然需要使用extern-claret-wee-alloc
,否则编译时会弹出错误:no-wee-alloc-external-claret
我答案末尾的代码对您不起作用?您使用的是哪个版本的Rust?这个答案没有提到您需要在货物中指定版本。toml
-否则,为了向后兼容,它默认为'2015'
。我认为缺少的区别是,安装了最新版本的Rust并不意味着选择的语言版本将是最新的。