In this Rust quickstart we will learn how to:

  • Install the Rust libSQL client
  • Connect to a remote Turso database
  • Execute a query using SQL
1

Install libsql-client

First begin by installing the libsql-client crate:

cargo add libsql-client
2

Set environment variables

Next export your LIBSQL_CLIENT_URL and LIBSQL_CLIENT_TOKEN:

  export LIBSQL_CLIENT_URL="libsql://"
  export LIBSQL_CLIENT_TOKEN="..."
3

Initialize a new client

Next initialize a new db client using libsql_client:

let db = libsql_client::Client::from_config(libsql_client::Config {
  url: url::Url::parse("libsql://").unwrap(),
  auth_token: None,
})
.await
.unwrap();

You can also connect to a local SQLite file by passing file: path instead, learn more.

4

Execute a query using SQL

You can execute a SQL query against your existing database by calling execute():

let result = client.execute("SELECT * FROM users").await?;

If you need to use placeholders for values, you can do that:

use libsql_client::{Statement, args};

let result = client.execute(Statement::with_args(
  "SELECT * FROM users WHERE id = ?",
  args!(1),
))
.await?;