I faced some troubles with borrow checker. Here is the code:
extern crate odbc_safe;
use odbc_safe::*;
fn main() {
let env = match Environment::new() {
Return::Success(env) => env,
_ => panic!(""),
};
let env = match env.declare_version_3_8() {
Return::Success(env) => env,
_ => panic!(""),
};
if let Return::Success(ds) = DataSource::with_parent(&env) {
}
}
You can see that it depends on odbc_safe
crate. I got failed to reproduce problem without this dependency, so the code is such is. Anyway, it doesn't compile with an error:
error[E0597]: `env` does not live long enough
--> src\main.rs:14:59
|
14 | if let Return::Success(ds) = DataSource::with_parent(&env) {
| ^^^ borrowed value does not live long enough
...
17 | }
| - `env` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
This behaviour is strange to me, since ds
dies at the end of if-let
and frees the reference to env
. But it's more strange that if I add a semicolon to the end of if-let statement, making it an expression, then the code compiles normally. Thus follow code does compile normally:
extern crate odbc_safe;
use odbc_safe::*;
fn main() {
let env = match Environment::new() {
Return::Success(env) => env,
_ => panic!(""),
};
let env = match env.declare_version_3_8() {
Return::Success(env) => env,
_ => panic!(""),
};
if let Return::Success(ds) = DataSource::with_parent(&env) {
};
}
As I said, I got no luck to reproduce the trouble with no odbc_safe
depenency. I tried to mimic types from it, but everything worked as expected.
rust version: stable-x86_64-pc-windows-gnu (default) rustc 1.33.0 (2aa4c46cf 2019-02-28)
odbc_safe version: 0.4.1
Any ideas what is going on?