为什么我需要使用self::core::ops?

8

我正在尝试使用来自coreMul

编译器建议使用这种方法,它可以正常工作:

extern crate core;

use self::core::ops::Mul;

but why doesn't

extern crate core;

use core::ops::Mul;

工作?

我遇到了错误error: 未解决的导入 `core::ops::Mul`。您是否想要使用 `self::core::ops`?


4
为什么不使用std::ops::Mul - mdup
2
@mdup 是因为编译器报错了 core,这是 Rust 中一个非常不明显的部分 :-( - Shepmaster
2个回答

14

extern crate x;x加载到当前命名空间中。use语句是绝对路径,除非它们以self::开头,因此如果您将extern crate core;放在crate根目录以外的任何位置,则需要指定绝对路径或使用self::

mod foo {
    mod bar {
        extern crate core;
        use foo::bar::core::ops::Mul;
        // or `use self::core::ops::Mul;`
        // or even `use super::bar::core::ops::Mul;` if you’re mad
        // but not `use core::ops::Mul;`.
    }
}

一般来说,你不应该直接使用core。其中所有稳定的内容都在std中自动包含。


0
原来我应该使用 use std::ops::Mul;

可以使用 libcore,适用于完全不使用 libstd 的情况。但是建议使用 libstd,因为它是 Rust 稳定通道目前公开的标准库。 - bluss

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接