Skip to content

Commit a49c590

Browse files
committed
Connect: Add Rust
1 parent 74d74f5 commit a49c590

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
r"https://qz.surister.dev/",
8989
# Read timed out.
9090
r"https://flowfuse.com/",
91+
# 404 Client Error: Not Found
92+
r"https://crates.io/crates/",
9193
]
9294

9395
linkcheck_anchors_ignore_for_url += [

docs/connect/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,18 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an
105105
```{image} /_assets/icon/ruby-logo.svg
106106
:height: 40px
107107
```
108+
::::
108109

110+
::::{grid-item-card} Rust
111+
:link: connect-rust
112+
:link-type: ref
113+
:link-alt: Connect to CrateDB using Rust
114+
:padding: 3
115+
:text-align: center
116+
:class-card: sd-pt-3
117+
:class-body: sd-fs-1
118+
:class-title: sd-fs-6
119+
{fab}`rust`
109120
::::
110121

111122
:::::
@@ -181,6 +192,7 @@ javascript
181192
php
182193
python
183194
ruby
195+
rust/index
184196
natural
185197
All drivers <drivers>
186198
```

docs/connect/rust/index.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
(connect-rust)=
2+
3+
# Rust
4+
5+
:::{div} sd-text-muted
6+
Connect to CrateDB from Rust applications.
7+
:::
8+
9+
:::{rubric} About
10+
:::
11+
12+
[postgres] is a synchronous Rust client for the PostgreSQL database.
13+
14+
:::{rubric} Synopsis (localhost)
15+
:::
16+
17+
`main.rs`
18+
```rust
19+
use postgres::{Client, NoTls};
20+
21+
fn main() -> Result<(), Box<dyn std::error::Error>> {
22+
let mut client = Client::connect("postgresql://crate@localhost:5432/?sslmode=disable", NoTls)?;
23+
24+
for row in client.query(
25+
"SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3",
26+
&[],
27+
)? {
28+
let mountain: &str = row.get(0);
29+
let height: i32 = row.get(1);
30+
println!("{}: {}", mountain, height);
31+
}
32+
Ok(())
33+
}
34+
```
35+
36+
:::{include} ../_cratedb.md
37+
:::
38+
```shell
39+
cargo init
40+
cargo add postgres
41+
cargo run
42+
```
43+
44+
:::{rubric} Synopsis (CrateDB Cloud)
45+
:::
46+
For CrateDB Cloud, add TLS support and update the connection string with
47+
your cluster details.
48+
49+
`main.rs`
50+
```rust
51+
use postgres::Client;
52+
use native_tls::TlsConnector;
53+
use postgres_native_tls::MakeTlsConnector;
54+
55+
fn main() -> Result<(), Box<dyn std::error::Error>> {
56+
let tls = MakeTlsConnector::new(TlsConnector::new()?);
57+
let mut client = Client::connect("postgresql://crate:<password>@<cluster-name>.<region>.cratedb.net:5432/?sslmode=require", tls)?;
58+
59+
for row in client.query(
60+
"SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3",
61+
&[],
62+
)? {
63+
let mountain: &str = row.get(0);
64+
let height: i32 = row.get(1);
65+
println!("{}: {}", mountain, height);
66+
}
67+
Ok(())
68+
}
69+
```
70+
```shell
71+
cargo init
72+
cargo add postgres postgres-native-tls native-tls
73+
cargo run
74+
```
75+
76+
77+
## Example
78+
79+
:::{card}
80+
:link: https://github.com/crate/cratedb-examples/tree/main/by-language/rust-postgres
81+
:link-type: url
82+
{material-outlined}`play_arrow;2em`
83+
Connecting to CrateDB with Rust.
84+
+++
85+
Demonstrates a basic example program that uses the Rust postgres package.
86+
:::
87+
88+
[![Rust](https://github.com/crate/cratedb-examples/actions/workflows/lang-rust-postgres.yml/badge.svg)](https://github.com/crate/cratedb-examples/actions/workflows/lang-rust-postgres.yml)
89+
90+
91+
[postgres]: https://crates.io/crates/postgres

0 commit comments

Comments
 (0)