mirror of
https://github.com/asg017/sqlite-vec.git
synced 2026-04-25 08:46:49 +02:00
docs
This commit is contained in:
parent
4febdff11a
commit
356f75cca7
17 changed files with 350 additions and 166 deletions
|
|
@ -1,4 +1,5 @@
|
|||
# Using `sqlite-vec` in Rust
|
||||
[](https://crates.io/crates/sqlite-vec)
|
||||
|
||||
You can embed `sqlite-vec` into your Rust projects using the official
|
||||
[`sqlite-vec` crate](https://crates.io/crates/sqlite-vec).
|
||||
|
|
@ -18,16 +19,29 @@ SQLite library's `sqlite3_auto_extension()` function. Here's an example with
|
|||
|
||||
```rs
|
||||
use sqlite_vec::sqlite3_vec_init;
|
||||
use rusqlite::{ffi::sqlite3_auto_extension};
|
||||
use rusqlite::{ffi::sqlite3_auto_extension, Result};
|
||||
|
||||
fn main() {
|
||||
fn main()-> Result<()> {
|
||||
unsafe {
|
||||
sqlite3_auto_extension(Some(std::mem::transmute(sqlite3_vec_init as *const ())));
|
||||
}
|
||||
// future database connection will now automatically include sqlite-vec functions!
|
||||
let db = Connection::open_in_memory()?;
|
||||
let vec_version: String = db.query_row("select vec_version()", &[v.as_bytes()], |x| x.get(0)?)?;
|
||||
|
||||
println!("vec_version={vec_version}");
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
A full [`sqlite-vec` Rust demo](#TODO) is also available.
|
||||
|
||||
## Working with vectors in Rust
|
||||
|
||||
If your vectors are provided as a `Vec<f32>` type, the [`zerocopy` crate](https://crates.io/crates/zerocopy) is recommended, specifically `zerocopy::AsBytes`. This will allow you to pass in vectors into `sqlite-vec` without any copying.
|
||||
|
||||
```rs
|
||||
let query: Vec<f32> = vec![0.1, 0.2, 0.3, 0.4];
|
||||
let mut stmt = db.prepare("SELECT vec_length(?)")?;
|
||||
stmt.execute(&[item.1.as_bytes()])?;
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue