How do I split a string in Rust?

前端 未结 5 1116
情深已故
情深已故 2020-11-30 19:18

From the documentation, it\'s not clear. In Java you could use the split method like so:

\"some string          


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 20:07

    Use split()

    let mut split = "some string 123 ffd".split("123");
    

    This gives an iterator, which you can loop over, or collect() into a vector.

    for s in split {
        println!("{}", s)
    }
    let vec = split.collect::>();
    // OR
    let vec: Vec<&str> = split.collect();
    

提交回复
热议问题