Skip to main content

Connect with Rust

To connect SingleStore with Rust, use SQLx with its MySQL feature. Refer to the following Cargo.toml file:

[package]
name = "rust-singlestore-connect"
version = "0.1.0"
edition = "2021"

[dependencies]
sqlx = { version = "0.5", features = ["runtime-tokio-native-tls", "mysql"] }
tokio = { version = "1", features = ["full"] }

For more information, see SQLx Docs.

Here's a sample main.rs file:

use sqlx::mysql::{MySqlConnectOptions, MySqlPoolOptions};

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    let opts = MySqlConnectOptions::new()
        .host("svc-ab8077f6-7b03-4ba3-b557-063c53eff943-ddl.aws-oregon-2.svc.singlestore.com")
        .username("admin")
        .password("PASSWORD");

    let pool = MySqlPoolOptions::new().connect_with(opts).await?;

    let row: (i64,) = sqlx::query_as("SELECT ?")
        .bind(150_i64)
        .fetch_one(&pool)
        .await?;

    assert_eq!(row.0, 150);

    Ok(())
}