为什么可以将带连字符的板条箱名称更改为带下划线的名称?在这种模棱两可的情况下命名规则是什么?
Why is changing hyphenated crate names to underscored names possible and what are the rules for naming under such ambiguous scenarios?
我创建了一个新的 Cargo 项目:cargo new --lib hyphen-crate
。
src/lib.rs
pub fn add(a: u32, b: u32) -> u32 {
a + b
}
tests/addition.rs
use hyphen_crate::add;
#[test]
fn addition_test() {
assert_eq!(5, add(2, 3));
}
Cargo.toml
[package]
name = "hyphen-crate"
version = "0.1.0"
authors = ["xolve"]
edition = "2018"
[dependencies]
我已经搜索并看到很多讨论是否应该允许在板条箱或包的名称中使用连字符,但没有 link 提到解决方案。
我看到的是 crate 名称 hyphen-crate
自动转换为 hyphen_crate
并且编译和测试成功。
包名称中允许使用连字符,但它们在内部被转换为下划线,因为连字符在 Rust identifiers.
中不是有效字符
似乎这种自动转换并非总是如此,带有连字符的 crate 必须手动重命名才能导入它们;例如。 extern crate "hyphen-crate" as hyphen_crate;
。有关详细信息,请参阅 Hyphens Considered Harmful RFC。
我创建了一个新的 Cargo 项目:cargo new --lib hyphen-crate
。
src/lib.rs
pub fn add(a: u32, b: u32) -> u32 {
a + b
}
tests/addition.rs
use hyphen_crate::add;
#[test]
fn addition_test() {
assert_eq!(5, add(2, 3));
}
Cargo.toml
[package]
name = "hyphen-crate"
version = "0.1.0"
authors = ["xolve"]
edition = "2018"
[dependencies]
我已经搜索并看到很多讨论是否应该允许在板条箱或包的名称中使用连字符,但没有 link 提到解决方案。
我看到的是 crate 名称 hyphen-crate
自动转换为 hyphen_crate
并且编译和测试成功。
包名称中允许使用连字符,但它们在内部被转换为下划线,因为连字符在 Rust identifiers.
中不是有效字符似乎这种自动转换并非总是如此,带有连字符的 crate 必须手动重命名才能导入它们;例如。 extern crate "hyphen-crate" as hyphen_crate;
。有关详细信息,请参阅 Hyphens Considered Harmful RFC。