Iterators functional ownership problem

Why does the commented lines below has ownership issues

pub fn scan_and_grab(p_conn: &mut PooledConn) -> Vec<(String, Option<u64>)> {
     let rows: Vec<String> = query from database;
    // rows.iter()
    //     .filter(|schema| schema.ne(&"ilis_standard"))
    //     .map(|r| {
    //         let version: Option<u64> = p_conn
    //             .query_first(format!("select version from {}.t_s_data_version;", r))
    //             .expect("Couldn't get {}'s data version");
    //         (r, version)
    //     })
    //     .collect::<Vec<(String, Option<u64>)>>()
    let mut tuples = vec![];
    for schema in rows {
        let version: Option<u64> = p_conn
                    .query_first(format!("select version from {}.t_s_data_version;", schema))
                    .expect("Couldn't get {}'s data version");
        tuples.push((schema, version))
    }
    tuples

iter() returns an iterator over borrowed elements. If you need the mapped iterator to yield owned Strings, clone r.

1 Like

Or use .into_iter() instead.

2 Likes

That doesn't work because the iterator is reused subsequently.

The uncommented portion of the OP is just an alternative that works without the correction as I understand it.

1 Like

Ah, I see, you are right.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.