I'm trying to use some xcb bindings while trying to learn Rust.
conn.get_setup()
only borrows an immutable reference; doesn't that borrow end after the function call ends?
use std::error::Error;
use std::convert::From;
pub struct State<'a> {
conn: xcb::Connection,
screen: xcb::Screen<'a>,
}
impl<'a> State<'a> {
pub fn new() -> Result<State<'a>, Box<dyn Error>> {
let (conn, _) = xcb::Connection::connect(None)?;
let screen = conn.get_setup().roots().next().ok_or::<Box<dyn Error>>(From::from("Couldn't get screen"))?;
Ok(State{conn: conn, screen:screen})
}
}
The compiler gives me
error[E0515]: cannot return value referencing local variable `conn`
--> /.../src/lib.rs:16:4
|
15 | let screen = conn.get_setup().roots().next().ok_or::<Box<dyn Error>>(From::from("Couldn't get screen"))?;
| ---- `conn` is borrowed here
16 | Ok(State{conn: conn, screen:screen})
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function
error[E0505]: cannot move out of `conn` because it is borrowed
--> /.../src/lib.rs:16:19
|
12 | impl<'a> State<'a> {
| -- lifetime `'a` defined here
...
15 | let screen = conn.get_setup().roots().next().ok_or::<Box<dyn Error>>(From::from("Couldn't get screen"))?;
| ---- borrow of `conn` occurs here
16 | Ok(State{conn: conn, screen:screen})
| ---------------^^^^-----------------
| | |
| | move out of `conn` occurs here
| returning this value requires that `conn` is borrowed for `'a`
Is there any way to return both conn
and state
or am I just limited to conn
?