#borrowed value does not live long enough

12 messages · Page 1 of 1 (latest)

radiant mantle
ember widget
#

that builder interface requires you to provide strings that are borrowed from a source that outlives the builder

#

that means that you need 2 loops, one to format the strings (which must be put in a Vec or something), and another one to call the builder

#

all of the .to_string().clone() is unnecessary and irrelevant — the problem is that constraint is a local String inside the loop, but where_clause needs to be given a borrow of something that outlives the loop

radiant mantle
#

Ok. I try with two loops now.

#

Hmm in second loop I get the same

            let mut constraints: Vec<String> = vec![];
            for (i, filter) in req.filters.iter().enumerate() {
                let constraint = format!("{} {} ${}", filter.column, filter.operator, i + 1);
                constraints.push(constraint);
            };
            for constraint in constraints {
                select = select.where_clause(&constraint);
            }
#

seems solved by combining it myself in one string instead of second loop.

ember widget
#

you would need to fill constraints before you create the select builder

radiant mantle
#

Does not help. Or I do somthing wrong :

let query_str = {
    let select_str = req.columns.iter().map(|x| x.name.to_string()).collect::<Vec<_>>().join(",");
    let mut constraints: Vec<String> = vec![];

    for (i, filter) in req.filters.iter().enumerate() {
        let constraint = format!("{} {} ${}", filter.column, filter.operator, i + 1);
        constraints.push(constraint);
    };

    let mut select = sql_query_builder::Select::new()
        .select(&select_str)
        .from(req.table.as_str());

    for constraint in constraints {
        select = select.where_clause(&constraint);
    }
    select.as_string()
};
#

still second loop gives error.

#

As I see it complains on using constraint, because it is destroyed in loop body.

ember widget
#

oh. you need for constraint in &constraints to do that