If i wrap a function around a diesel query against a table, what return type should i annotate?

Hello, Rustaceans!

I've recently started exploring Rust and am absolutely enamored by its capabilities and safety features. Currently, I'm developing a web service and need to load configuration settings from a MySQL database during the startup phase. I'm considering encapsulating this functionality within a function for better structure.

Here's where I need some guidance: I'm struggling to determine the appropriate return type for my function because the type of the results variable isn't explicitly clear to me. This is somewhat perplexing as I'm still getting accustomed to Rust's type system.

Below is the function I'm working on:

fn acquire_configurations() -> Result<Vec<Configurations>, diesel::result::Error> {
    use self::schema::configurations::dsl::*;

    let connection = &mut establish_connection();

    let results = configurations
        .filter(version.eq("1.3.0"))
        .select(Configurations::as_select())
        .load(connection)
        .expect("Error when loading configurations");

    // for result in results {
    //     println!("{}", result.id);
    //     println!("{}", result.version);
    //     println!("{}", result.data);
    // }

    return Ok(results)

}

The Result type in Rust is used for error handling, that is, when the operation can fail. Result is an enum with two variants: Ok and Err, and there are many ways to compose them.

It's unclear if your confusion comes from the type system or with the error handling mechanics. It would be great if you could share more details on both your background and what part(s) specifically are you struggling with, so that we can better assist you.

Do you get an error when you try to compile that function? What’s the full compiler output? (Run cargo check in the project directory, in a terminal.)

using expect in a function that returns Result is usually wrong, use .load(connection)?; instead. this will return early if an error is encountered, unwrapping the Ok value otherwise.

the one advantage of failing early like this is it can make it easier to spot where there error originated.

1 Like

Thanks for your replies!

I had just figured this out. Since I just want the Configurations type to be returned by the function, I added code below to make this happen:

if let Some(result) = results.get(0) {
        println!("Configuration version: {} loaded", configuration_version);
        return Ok(result.clone());
    } else {
        println!(
            "No configurations available for version: {}", configuration_version
        );
        return Err(diesel::result::Error::NotFound)
    }

Now it works fine!

Btw, I am a python developer and I wrote C++ codes for optimizing performances. So in terms of low level programming, I may need some time to accustom to. But anyways, really appreciated your helps and explanations on the return type!

Don't use explicit return if you are not returning early. Rust is an expression language.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.