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 114 views
1 answer 128 views
1 answer 139 views
1 answer 154 views
154 views asked May 30, 2023 by avibootz
1 answer 116 views
3 answers 171 views
171 views asked May 30, 2023 by avibootz
...