How to split a string by newline in Rust

2 Answers

0 votes
fn main() {
    let s : String = "rust\njava\nc#\nc\npython".to_string();
    
    let lines : Vec<&str> = s.split("\n").collect();

    println!("{:#?}", lines);
}



 
/*
run:
   
[
    "rust",
    "java",
    "c#",
    "c",
    "python",
]

*/

 



answered May 30, 2023 by avibootz
0 votes
fn main() {
    let s : String = "rust\njava\nc#\nc\npython".to_string();
    
    let lines : Vec<&str> = s.split("\n").collect();

    for st in &lines {
        println!("{}", st);
    }
}



 
/*
run:
   
rust
java
c#
c
python

*/

 



answered May 30, 2023 by avibootz

Related questions

1 answer 122 views
1 answer 135 views
1 answer 146 views
1 answer 163 views
163 views asked May 30, 2023 by avibootz
1 answer 123 views
3 answers 186 views
186 views asked May 30, 2023 by avibootz
...