3

I'm having a hard time figuring out where do I need to write something to enable this feature.

I tried adding #![feature(str_split_once)] to the file where I'm using it but nothing happens. By Googling I found How do you enable a Rust "crate feature"? but after adding

[features]
default = ["str_split_once"]

to Cargo it doesn't build with

Caused by: feature default includes str_split_once which is neither a dependency nor another feature

trent
  • 25,033
  • 7
  • 51
  • 90
ditoslav
  • 4,563
  • 10
  • 47
  • 79
  • [Cannot reproduce, make sure you're using nightly.](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=778fb2cb60bd0e2d8b351f02dbd29634) Also make sure you also add them to the root source file of the crate (`lib.rs` or `main.rs`). – Aplet123 Feb 18 '21 at 13:56
  • 1
    `str_split_once` is a language feature, not a crate feature. It has nothing to do with Cargo.toml. – SOFe Feb 18 '21 at 13:59

1 Answers1

3

I tried adding #![feature(str_split_once)] to the file where I'm using it but nothing happens.

I guess there's not really "nothing happens", but there was a warning similar to this one:

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

Playground

Just add this line at the beginning of lib.rs and/or main.rs, build with nightly, and it should work:

#![feature(str_split_once)]

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

Playground

Cerberus
  • 8,879
  • 1
  • 25
  • 40