处理克隆

Dealing with clone

我正在尝试迭代 VCF 文件并使用数据创建向量以构建 DataFrame。

但是,rust 编译器报错说借用的值不够活。

我正在克隆该值,因为在那种情况下,我借用了 vcf 记录,同时作为不可变记录和可变记录。

我不知道如何进行。下面是我的代码片段和错误。

use flate2::read::MultiGzDecoder;
use std::fs::File;
use std::io::BufReader;
mod vcf_reader;
use vcf::{VCFReader, U8Vec, VCFHeaderFilterAlt, VCFError, VCFRecord};

use vcf_reader::reader::VCFSamples;
use polars_core::prelude::*;
use std::time::Instant;
    
fn main() -> Result<()>  {
    let now = Instant::now();
    let mut reader = VCFReader::new(BufReader::new(File::open(
       "C:\Users\Desktop\rust_lectures\vcf_reader\vcf_reader\*.vcf"
    )?)).unwrap();


    // prepare VCFRecord object
    let mut vcf_record = VCFRecord::new(reader.header());

    // read one record
    let mut chromosome = Vec::new();
    let mut position = Vec::new();
    let mut id = Vec::new();
    let mut reference = Vec::new();
    let mut alternative = Vec::new();
    let result: bool = reader.next_record(&mut vcf_record).unwrap();
    loop {
        let row = vcf_record.clone();
        if result == false {
            let df = df!(
                "Chromosome" => chromosome,
                "Position" => position,
                "Id" => id,
                "Reference" => reference,
                "Alternative" => alternative,
        
            );
            println!("{:?}", df);
            let elapsed = now.elapsed();
            println!("Elapsed: {:.2?}", elapsed);
            return Ok(())
        } else {
            
            chromosome.push(String::from_utf8_lossy(&row.chromosome));
            position.push(&row.position);
            id.push(String::from_utf8_lossy(&row.id[0]));
            reference.push(String::from_utf8_lossy(&row.reference));
            alternative.push(String::from_utf8_lossy(&row.alternative[0]));
            
        }
        reader.next_record(&mut vcf_record).unwrap();
        
    }
error[E0597]: `row.chromosome` does not live long enough
  --> src\main.rs:44:53
   |
44 |             chromosome.push(String::from_utf8_lossy(&row.chromosome));
   |             ----------------------------------------^^^^^^^^^^^^^^^^^^^^--
   |             |                                       |
   |             |                                       borrowed value does not live long enough
   |             borrow later used here
...
50 |         }
   |         - `row.chromosome` dropped here while still borrowed

I'm cloning the value because

问题是您正在克隆记录,然后将引用存储到数组中。由于这些是对克隆记录的引用,因此它们只存在于块末尾。

所以要么:

  • 属性从克隆中移出(基本上将其分解)
  • 或者不使用记录的副本,而是从基础中复制单个字段 vcf_record

无论哪种方式,你也在滥用 from_utf8_lossy:它总是 returns a reference-ish,因为如果输入是有效的 utf8,它会避免分配(在这种情况下,它基本上只是 returns对原始数据的引用)。