Rust driver
The Rust driver was developed by the TypeDB team to enable TypeDB support for Rust software and developers.
Install
See the Version Compatibility table to check what versions of TypeDB and Rust driver are compatible.
The minimum version of Rust is 1.68.0
.
For Linux: the minimum version of glibc
is 2.25.0
.
To install TypeDB Rust driver, select sync
or async
and follow the instructions below:
-
async
-
sync
cargo add typedb-driver
cargo add typedb-driver -F sync
Quickstart
See below a short example of Rust code that connects to a local TypeDB server,
creates a database named access-management-db
, defines a simple schema, inserts some data, and then reads it.
use typedb_driver::{
Connection, DatabaseManager, Error, Promise, Session, SessionType, TransactionType,
};
fn main() -> Result<(), Error> {
const DB_NAME: &str = "access-management-db";
const SERVER_ADDR: &str = "127.0.0.1:1729";
let driver = Connection::new_core(SERVER_ADDR)?;
let databases = DatabaseManager::new(driver);
if databases.contains(DB_NAME)? {
databases.get(DB_NAME)?.delete()?;
}
databases.create(DB_NAME)?;
{
let session = Session::new(databases.get(DB_NAME)?, SessionType::Schema)?;
let tx = session.transaction(TransactionType::Write)?;
tx.query().define("define person sub entity;").resolve()?;
tx.query().define("define name sub attribute, value string; person owns name;").resolve()?;
tx.commit().resolve()?;
}
{
let session = Session::new(databases.get(DB_NAME)?, SessionType::Data)?;
{
let tx = session.transaction(TransactionType::Write)?;
let _ = tx.query().insert("insert $p isa person, has name 'Alice';")?;
let _ = tx.query().insert("insert $p isa person, has name 'Bob';")?;
tx.commit().resolve()?;
}
{
let tx = session.transaction(TransactionType::Read)?;
let res = tx.query().fetch("match $p isa person; fetch $p: name;").unwrap();
for item in res {
println!("{}", &item.unwrap().to_string());
}
Ok(())
}
}
}
The above example is written for the sync
Rust.
For more code examples, see the Tutorial and API reference links below.
Learn more
Any questions about the driver after reading the documentation? Feel free to ask on our Discord community server. |