Skip to content

Commit 4d87608

Browse files
committed
Connect: Add Rust
1 parent 5f5906b commit 4d87608

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-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
@@ -110,7 +110,18 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an
110110
```{image} /_assets/icon/ruby-logo.svg
111111
:height: 40px
112112
```
113+
::::
113114

115+
::::{grid-item-card} Rust
116+
:link: connect-rust
117+
:link-type: ref
118+
:link-alt: Connect to CrateDB using Rust
119+
:padding: 3
120+
:text-align: center
121+
:class-card: sd-pt-3
122+
:class-body: sd-fs-1
123+
:class-title: sd-fs-6
124+
{fab}`rust`
114125
::::
115126

116127
:::::
@@ -187,6 +198,7 @@ javascript
187198
php
188199
python
189200
ruby
201+
rust/index
190202
natural
191203
All drivers <drivers>
192204
```

docs/connect/rust/index.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
Start CrateDB using Docker or Podman, then compile and invoke example program.
37+
```shell
38+
docker run --rm --publish=5432:5432 crate '-Cdiscovery.type=single-node'
39+
```
40+
```shell
41+
cargo init
42+
cargo add postgres
43+
cargo run
44+
```
45+
46+
:::{rubric} Synopsis (CrateDB Cloud)
47+
:::
48+
For CrateDB Cloud, add TLS support and update the connection string with
49+
your cluster details.
50+
51+
`main.rs`
52+
```rust
53+
use postgres::Client;
54+
use native_tls::TlsConnector;
55+
use postgres_native_tls::MakeTlsConnector;
56+
57+
fn main() -> Result<(), Box<dyn std::error::Error>> {
58+
let tls = MakeTlsConnector::new(TlsConnector::new()?);
59+
let mut client = Client::connect("postgresql://crate:<password>@<cluster-name>.<region>.cratedb.net:5432/?sslmode=require", tls)?;
60+
61+
for row in client.query(
62+
"SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3",
63+
&[],
64+
)? {
65+
let mountain: &str = row.get(0);
66+
let height: i32 = row.get(1);
67+
println!("{}: {}", mountain, height);
68+
}
69+
Ok(())
70+
}
71+
```
72+
```shell
73+
cargo init
74+
cargo add postgres postgres-native-tls native-tls
75+
cargo run
76+
```
77+
78+
79+
## Example
80+
81+
:::{card}
82+
:link: https://github.com/crate/cratedb-examples/tree/main/by-language/rust-postgres
83+
:link-type: url
84+
{material-outlined}`play_arrow;2em`
85+
Connecting to CrateDB with Rust.
86+
+++
87+
Demonstrates a basic example program that uses the Rust postgres package.
88+
:::
89+
90+
[![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)
91+
92+
93+
[postgres]: https://crates.io/crates/postgres

0 commit comments

Comments
 (0)