How do I iterate over a Split<&Str>

Hi, I got stuck while working on my first project in Rust

My code:

    let mut index = 0;
    for i in l_a {
        let  sne = i.split(" ");
        for a in sne {
            print!("{} ",m[index]);
            index+=1;
            if a == sne[0] {
                print!("\n")
            }
        }
    }

So right here I am splitting a string but I want to check if a the iterator is the last value of sne the splitted string but it gives me an error:

error[E0608]: cannot index into a value of type `std::str::Split<'_, &str>`

Kindly help me
This project means very much to me
In case you want the full code there is a github gist below
https://gist.github.com/Saad-py/a42f0c30221ea37f3fdce1831353d741

You might want to do something like this, with the Iterator::enumerate method. This will print a newline after the first word in each line:

        for (pos, a) in sne.enumerate() {
            print!("{} ",m[index]);
            index+=1;
            if pos == 0 {
                print!("\n")
            }
        }

index 0 is the first element, not the last element. If you want to print a newline after the last word in each line, you can put a print! statement after the loop instead:

        for a in sne {
            print!("{} ",m[index]);
            index+=1;
        }
        print!("\n")

By the way, it looks like you could simplify this program by doing the work in a single loop, instead of two separate loops:

fn main() {
    let stop_words = ["but","has","the", "is", "a", "an", "they", "and", "I", "in", "there", "was", "who", "The", "know", "it"];
    let file = std::fs::read_to_string("src/Text.txt").expect("Couldn't read file");
    
    for line in file.lines() {
        for word in line.split(" ") {
            if stop_words.contains(&word) {
                print!("{} ", word.to_lowercase());
            } else {
                print!("{} ", word.to_uppercase());
            }
        }
        print!("\n");
    }
}

Thanks @mbrubeck , but you know the solution would be same and I am really proud of what I made and i also fixed the issue by creating another vector and having all the case sensitive words in it and to print new line I just did (vector name = ls)

if a == ls[ls.len() - 1] { print!("\n") }

And Thanks Again
But If you like you could reply on another topic like this to help me out
And on that topic I am just asking you to give me ideas for projects that would help me make myself comfortable with rust.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.