I recently posted an account of a battle I had with the Rust compiler, which got explained by some helpful folks who understand the language and the compiler better than I do. At the time, I said that I was going to take a hiatus from Rust, hoping for improvement in both the compiler diagnostics and the documentation.
Well, I had some time, unexpectedly, and couldn't resist trying to continue solving the problems I was having with the program I was working on. I made some progress until I ran into the following:
// Prepare to build the account tree
let marketable_asset_value_stmt = db.prepare(constants::MARKETABLE_ASSET_VALUE_SQL).unwrap();
let non_marketable_asset_and_liability_value_stmt = db.prepare(constants::NON_MARKETABLE_ASSET_AND_LIABILITY_VALUE_SQL).unwrap();
let income_and_expenses_value_stmt = db.prepare(constants::INCOME_AND_EXPENSES_VALUE_SQL).unwrap();
let account_children_stmt = db.prepare(constants::ACCOUNT_CHILDREN_SQL).unwrap();
{
let mut account_value_statements = AccountValueStatements {
360 marketable_asset_value_stmt:&mut marketable_asset_value_stmt,
non_marketable_asset_and_liability_value_stmt:&mut non_marketable_asset_and_liability_value_stmt,
income_and_expenses_value_stmt:& mut income_and_expenses_value_stmt,
account_children_stmt:& mut account_children_stmt
};
//build_account_tree(&mut root, &mut account_value_statements, julian_begin_date_time, julian_end_date_time);
}
}
I've added line 360 to the code, so you will understand the compiler messages. Also note that the last close-brace terminates the main program.
Thisresults in the following error (one of several) with version 1.20 on an Arch Linux system:
error[E0597]: `marketable_asset_value_stmt` does not live long enough
--> src/main.rs:367:1
|
360 | marketable_asset_value_stmt:&mut marketable_asset_value_stmt,
| --------------------------- borrow occurs here
...
367 | }
| ^ `marketable_asset_value_stmt` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
The compiler is complaining the a reference (within the struct) to a statement outlives the statement. But the statement is bound in the outer scope and the struct is bound in the inner scope and therefore the struct should be deallocated before the statement and thus its reference. Either I've got this completely wrong or this is a compiler bug or .... Please explain. (Note that I've commented out the call to build_account_tree, which references the struct, in an effort to simplify things.)