【问题标题】:How to enable unstable Rust feature str_split_once?如何启用不稳定的 Rust 功能 str_split_once?
【发布时间】:2021-03-23 06:38:41
【问题描述】:

我很难弄清楚我需要在哪里写一些东西来启用这个功能。

我尝试将#![feature(str_split_once)] 添加到我正在使用它的文件中,但没有任何反应。通过谷歌搜索,我找到了How do you enable a Rust "crate feature"?,但添加后

[features]
default = ["str_split_once"]

对 Cargo 来说,它不是用它构建的

原因:功能 default 包括 str_split_once 这是 既不是依赖也不是其他特性

【问题讨论】:

标签: rust


【解决方案1】:

我尝试将 #![feature(str_split_once)] 添加到我正在使用它的文件中,但没有任何反应。

我猜并不是真的“什么都没发生”,但是有一个类似于这个的警告:

warning: crate-level attribute should be in the root module
 --> src/lib.rs:2:5
  |
2 |     #![feature(str_split_once)]
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Playground

只需在 lib.rs 和/或 main.rs 的开头添加这一行,用 nightly 构建,它应该可以工作:

#![feature(str_split_once)]

fn main() {
    // prints Some(("test1", "test2 test3"))
    println!("{:?}", "test1 test2 test3".split_once(" "));
}

Playground

【讨论】:

  • 原来我们使用的是 stable 所以我不能使用这个功能,因为我必须使用 nightly 才能使用它
  • 是的,每种语言功能仅在夜间可用,因为它可能会在任何版本中发生变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
  • 1970-01-01
  • 2020-09-29
  • 1970-01-01
相关资源
最近更新 更多