Value used here after partial move

Hello

I think I have a very basic typical Rust error. But I don't understand it all. Can someone explain on how to solve this issue, and what's the cause of it?

Code:

    //Function to establish (a pooled) connection with the MySQL database. This connection
    //is stored in self.conn
    pub fn init_connection(database_name : &String, database_user : &String, database_pass : &String) -> std::result::Result<mysql::Pool, mysql::Error> {
        //Create connection with database
        let conn_str : String = format!("mysql://{user}:{pass}@localhost:3307/{name}", user=database_user, pass=database_pass, name=database_name);
        let conn : std::result::Result<mysql::Pool, mysql::Error> = mysql::Pool::new(conn_str);

        //Check if connection was succesful
        match conn {
            Ok(t) => {
                println!("Connection to {} succesfully made...", database_name);
                conn
            },
            Err(e) => {
                eprintln!("Connection to {} failed: {}", database_name, e.description());
                std::process::exit(-1);
            },
        }
    }

Error:

error[E0382]: use of moved value: `conn`
   --> src/main.rs:327:21
    |
325 |                 Ok(t) => {
    |                    - value moved here
326 |                     println!("Connection to {} succesfully made...", database_name);
327 |                     conn
    |                     ^^^^ value used here after partial move
    |
    = note: move occurs because value has type `mysql::Pool`, which does not implement the `Copy` trait

Thanks!

The way you've matched completely destructures that Result, with Ok(t) moving the value into a new local t. Since you're not actually using that local, you could just match Ok(_) instead.

Another option is to just match the error case:

    if let Err(e) = conn {
        eprintln!("Connection to {} failed: {}", database_name, e.description());
        std::process::exit(-1);
    }
    println!("Connection to {} succesfully made...", database_name);
    conn
2 Likes