How to iterate and extract values out of a for loop in Rust

前端 未结 2 1467
情歌与酒
情歌与酒 2020-12-21 01:05

I\'m new to Rust and looking to understand concepts like borrowing. I\'m trying to create a simple two dimensional array using standard input. The code:

use          


        
相关标签:
2条回答
  • 2020-12-21 01:45

    This answer was moved from the question, where it solved the OPs needs.

    use std::io;
    
    fn main() {
        let mut values = vec![vec![String::new(); 6]; 6];
        for i in 0..6 {
            let mut outputs = String::new();
            io::stdin().read_line(&mut outputs)
                    .expect("failed to read line");
    
            let values_itr = outputs.trim().split(' ');
            let mut j = 0;
            for (_, value) in values_itr.enumerate() {
                values[i][j] = value.to_string();
                j += 1;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-21 01:46

    split() gives you substrings (string slices) borrowed from the original string, and the original string is outputs from line 6.

    1. The string slices can't outlive the scope of outputs: when a loop iteration ends, outputs is deallocated. Since values is longer lived, the slices can't be stored there.
    2. We can't borrow slices of outputs across a modification of outputs. So even if the String outputs itself was defined before values, we couldn't easily put the string slices from .split() into values; modifying the string (reading into it) invalidates the slices.

    A solution needs to either

    • Use a nested array of String, and when you assign an element from the split iterator, make a String from the &str using .to_string(). I would recommend this solution. (However an array of String is not at as easy to work with, maybe already this requires using Vec instead.) 1
    • Read all input before constructing a nested array of &str that borrows from the input String. This is good if the nested array is something that you only need temporarily.

    1: You can use something like vec![vec![String::new(); 6]; 6] instead

    0 讨论(0)
提交回复
热议问题