ditoslav
ditoslav

Reputation: 4872

How to enable unstable Rust feature str_split_once?

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

Upvotes: 3

Views: 5003

Answers (1)

Cerberus
Cerberus

Reputation: 10247

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

Upvotes: 3

Related Questions