1
votes

Full error:

  --> src\readers.rs:10:30
   |
5  | pub fn get_sequences(raw_sequences: &str) -> Result<Vec<Cow<[u8]>>, Error> {
   |                                     - let's call the lifetime of this reference `'1`
...
10 |     while let Some(result) = reader.next() {
   |                              ^^^^^^ `reader` was mutably borrowed here in the previous iteration of the loop
...
15 |     Ok(sequences)
   |     ------------- returning this value requires that `reader` is borrowed for `'1`

Code:

pub fn get_sequences(raw_sequences: &str) -> Result<Vec<Cow<[u8]>>, Error> {
    let bytes = raw_sequences.as_bytes();
    let mut reader = Reader::new(BufReader::new(bytes));
    let mut sequences: Vec<Cow<[u8]>> = vec![];

    while let Some(result) = reader.next() {
        let record = result?;
        sequences.push(record.full_seq());
    }

    Ok(sequences)
}

I'm using the SeqIO crate (https://github.com/markschl/seq_io/tree/v0.3)

Honestly not sure how to fix this. Any help would be appreciated

1
full_seq() doesn't copy the data, it just stores a reference to the reader's buffer, which could become invalid the next time you call reader.next(), so the compiler won't let you call reader.next() while that reference still exists. Maybe try using owned_seq() instead? That should make an owned copy of the data to store in sequences so that the reader can reuse/overwrite the buffer when you call reader.next().Coder-256

1 Answers

0
votes

the fix is change full_seq to owned_seq, and return Vec<Vec> instead of Vec<Cow<[u8]>>, the is because record do have a reference to reader, and full_seq return a Cow type that has implicit reference to record, thus it has reference to the reader which is a local variable

pub fn get_sequences(raw_sequences: &str) -> Result<Vec<Vec<u8>>, Error> {
    let bytes = raw_sequences.as_bytes();
    let mut reader = Reader::new(bytes);
    let mut sequences = vec![];

    while let Some(result) = reader.next() {
        let record = result?;
        sequences.push(record.owned_seq());
    }

    Ok(sequences)
}