how to get element from string that was split

Viewed 31
fn main() {
    let x:String="ggg\nggg\nggg".to_owned();
    let lines: Vec<&str> = x.split('\n').collect();
    for i in lines{
        println!("{}",i);
    }
    println!("{}",**(lines.get(0)));
}

I want to make a game and x is a map representation (g=grass...) and i need to know the char in specific spot for collision checking. How do i do it?

The code above returns: error[E0614]: type Option<&&str> cannot be dereferenced

1 Answers

You can use unwrap or expect or a match statement to handle the option, other than that, you can use the index access operator.

fn main() {
    let x:String="aaa\nbbb\nccc".to_owned();
    let lines: Vec<&str> = x.split('\n').collect();
    for i in &lines{
        println!("{}",i);
    }
    println!("{}",lines.get(0).expect("not enough elements"));
    println!("{}",lines[0]);
}

Related