Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在rust中成对阅读标准DIN中的行_Rust_Stdin - Fatal编程技术网

在rust中成对阅读标准DIN中的行

在rust中成对阅读标准DIN中的行,rust,stdin,Rust,Stdin,我的输入数据结构如下: label_1 value_1 label_2 value_2 ... 我的最终目标是将数据读入HashMap 我目前的工作方法是将偶数行和奇数行放在两个单独的向量中,然后从两个向量中读取以添加到Hashmap 使用std::io; 使用std::io::prelude::*; 使用std::collections::HashMap; fn main(){ 让mut标签:Vec=Vec::new(); 让mut值为:Vec=Vec::new(); 设stdin=io:

我的输入数据结构如下:

label_1
value_1
label_2
value_2
...

我的最终目标是将数据读入
HashMap

我目前的工作方法是将偶数行和奇数行放在两个单独的向量中,然后从两个向量中读取以添加到Hashmap

使用std::io;
使用std::io::prelude::*;
使用std::collections::HashMap;
fn main(){
让mut标签:Vec=Vec::new();
让mut值为:Vec=Vec::new();
设stdin=io::stdin();
/*读取从标准输入数据通过管道传输的线路*/
对于stdin.lock().lines().enumerate()中的(i,line){
如果i%2==0{
/*在标签向量中存储标签(偶数行)*/
labels.push(line.unwrap());
}否则{
/*在向量值中存储值(奇数行)*/
value.push(line.unwrap());
}
}
println!(“标签数:{}”,labels.len());
println!(“值的数目:{}”,values.len());
/*将标签和值压缩到一个迭代器中*/
让double_iter=labels.iter().zip(values.iter());
/*将(标签:值)对插入hashmap*/
让mut记录:HashMap=HashMap::new();
用于(标签、值)在双\u iter中{
记录。插入(标签、值);
}
}
我想问一下,如何在不经过向量中间步骤的情况下实现此结果?

如何:

fn main(){
设直线=vec![1,2,3,4,5,6];
让mut records=std::collections::HashMap::new();
对于(0..lines.len())中的i。步进(2){
//确保'i+1'存在
println!(“{}{}”,行[i],行[i+1]);
记录。插入(行[i],行[i+1]);
}
}
那么:

fn main(){
设直线=vec![1,2,3,4,5,6];
让mut records=std::collections::HashMap::new();
对于(0..lines.len())中的i。步进(2){
//确保'i+1'存在
println!(“{}{}”,行[i],行[i+1]);
记录。插入(行[i],行[i+1]);
}
}

您可以使用
.next()


您可以使用
.next()

您可以从
itertools
板条箱中使用:

use itertools::Itertools;
use std::io::{stdin, BufRead};

fn main() {
    for (label, value) in stdin().lock().lines().tuples() {
        println!("{}: {}", label.unwrap(), value.unwrap());
    }
}
另见:

  • 关于“是否存在用于迭代器在对、三元组等上循环的slice::chunks/windows等价物?”
您可以从
itertools
板条箱中使用:

use itertools::Itertools;
use std::io::{stdin, BufRead};

fn main() {
    for (label, value) in stdin().lock().lines().tuples() {
        println!("{}: {}", label.unwrap(), value.unwrap());
    }
}
另见:

  • 关于“是否存在用于迭代器在对、三元组等上循环的slice::chunks/windows等价物?”

如果您已经有一个向量,那么您也可以使用它。如果您已经有一个向量,那么您也可以使用它。请注意,当有一个未配对的行时,它会自动失败,而不像。请注意,当有一个未配对的行时,它会自动失败,而不像。