Skip to content

Commit 1b376f2

Browse files
Merge pull request #2169 from redis/DOC-5760-rust-landing-tces
DOC-5760 started doc support for Rust
2 parents 5b72ee4 + f5b9fc8 commit 1b376f2

File tree

11 files changed

+330
-8
lines changed

11 files changed

+330
-8
lines changed

build/components/example.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
'go': '//',
2727
'c#': '//',
2828
'redisvl': '#',
29-
'php': '//'
29+
'php': '//',
30+
'rust': '//',
31+
'rust-sync': '//',
32+
'rust-async': '//'
3033
}
3134

3235

build/local_examples.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
'.go': 'go',
2626
'.cs': 'c#',
2727
'.java': 'java',
28-
'.php': 'php'
28+
'.php': 'php',
29+
'.rs': 'rust'
2930
}
3031

3132
# Language to client name mapping (from config.toml clientsExamples)
@@ -36,7 +37,8 @@
3637
'c#': 'C#',
3738
'java': 'Java-Sync', # Default to sync, could be overridden
3839
'php': 'PHP',
39-
'redisvl': 'RedisVL'
40+
'redisvl': 'RedisVL',
41+
'rust': 'Rust-Sync'
4042
}
4143

4244

@@ -65,6 +67,11 @@ def get_client_name_from_language_and_path(language: str, path: str) -> str:
6567
return 'Java-Async'
6668
if 'lettuce-reactive' in path:
6769
return 'Java-Reactive'
70+
if language == 'rust':
71+
if 'rust-async' in path:
72+
return 'Rust-Async'
73+
if 'rust-sync' in path:
74+
return 'Rust-Sync'
6875
# Default behavior for all languages (and Java fallback)
6976
return get_client_name_from_language(language)
7077

config.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ tagManagerId = "GTM-TKZ6J9R"
4545
gitHubRepo = "https://github.com/redis/docs"
4646

4747
# Display and sort order for client examples
48-
clientsExamples = ["Python", "Node.js", "Java-Sync", "Java-Async", "Java-Reactive", "Go", "C#", "RedisVL", "PHP"]
48+
clientsExamples = ["Python", "Node.js", "Java-Sync", "Java-Async", "Java-Reactive", "Go", "C#", "RedisVL", "PHP", "Rust-Sync", "Rust-Async"]
4949
searchService = "/convai/api/search-service"
5050
ratingsService = "/docusight/api/rate"
5151

@@ -67,6 +67,8 @@ rdi_current_version = "1.14.1"
6767
"C#"={quickstartSlug="dotnet"}
6868
"RedisVL"={quickstartSlug="redis-vl"}
6969
"PHP"={quickstartSlug="php"}
70+
"Rust-sync"={quickstartSlug="rust"}
71+
"Rust-async"={quickstartSlug="rust"}
7072

7173
# Markup
7274
[markup]

content/develop/clients/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ weight: 30
1919

2020
Use the Redis client libraries to connect to Redis servers from
2121
your own code. We document the following client libraries
22-
for seven main languages:
22+
for eight main languages:
2323

2424
| Language | Client name | Docs | Supported |
2525
| :-- | :-- | :-- | :-- |
@@ -32,6 +32,7 @@ for seven main languages:
3232
| [Go](https://go.dev/) | [`go-redis`](https://github.com/redis/go-redis) | [`go-redis` guide]({{< relref "/develop/clients/go" >}}) | Yes |
3333
| [PHP](https://www.php.net/)| [`Predis`](https://github.com/predis/predis) | [`Predis` guide]({{< relref "/develop/clients/php" >}}) | No |
3434
| [C](https://en.wikipedia.org/wiki/C_(programming_language)) | [`hiredis`](https://github.com/redis/hiredis) | [`hiredis` guide]({{< relref "/develop/clients/hiredis" >}}) | Yes |
35+
| [Rust](https://www.rust-lang.org/) | [`redis-rs`](https://github.com/redis-rs/redis-rs) | [`redis-rs` guide]({{< relref "/develop/clients/rust" >}}) | No |
3536

3637
We also provide several higher-level
3738
[object mapping (OM)]({{< relref "/develop/clients/om-clients" >}})
@@ -51,7 +52,6 @@ Redis does not document directly:
5152
| [Dart](https://dart.dev/) | redis_dart_link | https://github.com/toolsetlink/redis_dart_link | https://github.com/toolsetlink/redis_dart_link |
5253
| [PHP](https://www.php.net/) | PhpRedis extension | https://github.com/phpredis/phpredis | https://github.com/phpredis/phpredis/blob/develop/README.md |
5354
| [Ruby](https://www.ruby-lang.org/en/) | redis-rb | https://github.com/redis/redis-rb | https://rubydoc.info/gems/redis |
54-
| [Rust](https://www.rust-lang.org/) | redis-rs | https://github.com/redis-rs/redis-rs | https://docs.rs/redis/latest/redis/ |
5555

5656

5757
## Requirements

content/develop/clients/hiredis/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories:
1212
description: Connect your C application to a Redis database.
1313
linkTitle: hiredis (C)
1414
title: hiredis guide (C)
15-
weight: 9
15+
weight: 10
1616
---
1717

1818
[`hiredis`](https://github.com/redis/hiredis) is the
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Connect your Rust application to a Redis database
13+
linkTitle: redis-rs (Rust)
14+
title: redis-rs guide (Rust)
15+
weight: 9
16+
---
17+
18+
[`redis-rs`](https://github.com/redis-rs/redis-rs) is the [Rust](https://www.rust-lang.org/) client for Redis.
19+
The sections below explain how to install `redis-rs` and connect your application to a Redis database.
20+
21+
{{< note >}}Although we provide basic documentation for `redis-rs`, it is a third-party
22+
client library and is not developed or supported directly by Redis.
23+
{{< /note >}}
24+
25+
`redis-rs` requires a running Redis server. See [here]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis Open Source installation instructions.
26+
27+
## Install
28+
29+
To use the synchronous API, add the `redis` crate as a dependency in your
30+
`Cargo.toml` file:
31+
32+
```toml
33+
[dependencies]
34+
redis = "0.32.5"
35+
```
36+
37+
If you want to use the asynchronous API, you should also enable either
38+
[`tokio`](https://tokio.rs/) or [`smol`](https://crates.io/crates/smol)
39+
as your async platform:
40+
41+
```toml
42+
[dependencies]
43+
# if you use tokio
44+
tokio = { version = "1.32.0", features = ["full"] }
45+
redis = { version = "0.32.5", features = ["tokio-comp"] }
46+
47+
# if you use smol
48+
smol = "2.0.2"
49+
redis = { version = "0.32.5", features = ["smol-comp"] }
50+
```
51+
52+
## Connect
53+
54+
Start by importing the `Commands` or `AsyncCommands` trait from the `redis` crate:
55+
56+
{{< clients-example set="landing" step="import" lang_filter="Rust-Sync,Rust-Async" >}}
57+
{{< /clients-example >}}
58+
59+
The following example shows the simplest way to connect to a Redis server:
60+
61+
{{< clients-example set="landing" step="connect" lang_filter="Rust-Sync,Rust-Async" >}}
62+
{{< /clients-example >}}
63+
64+
After connecting, you can test the connection by storing and retrieving
65+
a simple [string]({{< relref "/develop/data-types/strings" >}}):
66+
67+
{{< clients-example set="landing" step="set_get_string" lang_filter="Rust-Sync,Rust-Async" >}}
68+
{{< /clients-example >}}
69+
70+
You can also easily store and retrieve a [hash]({{< relref "/develop/data-types/hashes" >}}):
71+
72+
{{< clients-example set="landing" step="set_get_hash" lang_filter="Rust-Sync,Rust-Async" >}}
73+
{{< /clients-example >}}
74+
75+
## More information
76+
77+
See the [`redis-rs`](https://docs.rs/redis/latest/redis/) documentation
78+
and the [GitHub repository](https://github.com/redis-rs/redis-rs) for more
79+
information and examples.

data/components/index.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"jedis",
1616
"lettuce_async",
1717
"lettuce_reactive",
18-
"redis_vl"
18+
"redis_vl",
19+
"redis_rs_sync",
20+
"redis_rs_async"
1921
],
2022
"assets": [],
2123
"website": {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"id": "redis_rs_async",
3+
"type": "client",
4+
"name": "redis-rs-async",
5+
"language": "Rust-Async",
6+
"label": "Rust-Async",
7+
"repository": {
8+
"git_uri": "https://github.com/redis-rs/redis-rs"
9+
},
10+
"examples": {
11+
"git_uri": "https://github.com/redis-rs/redis-rs",
12+
"path": "redis/examples",
13+
"pattern": "*.rs"
14+
}
15+
}

data/components/redis_rs_sync.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"id": "redis_rs_sync",
3+
"type": "client",
4+
"name": "redis-rs-sync",
5+
"language": "Rust-Sync",
6+
"label": "Rust-Sync",
7+
"repository": {
8+
"git_uri": "https://github.com/redis-rs/redis-rs"
9+
},
10+
"examples": {
11+
"git_uri": "https://github.com/redis-rs/redis-rs",
12+
"path": "redis/examples",
13+
"pattern": "*.rs"
14+
}
15+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// EXAMPLE: landing
2+
// STEP_START import
3+
use redis::AsyncCommands;
4+
// STEP_END
5+
6+
#[tokio::main]
7+
async fn main() {
8+
// STEP_START connect
9+
let mut r = match redis::Client::open("redis://127.0.0.1") {
10+
Ok(client) => {
11+
match client.get_multiplexed_async_connection().await {
12+
Ok(conn) => conn,
13+
Err(e) => {
14+
println!("Failed to connect to Redis: {e}");
15+
return;
16+
}
17+
}
18+
},
19+
Err(e) => {
20+
println!("Failed to create Redis client: {e}");
21+
return;
22+
}
23+
};
24+
// STEP_END
25+
26+
// STEP_START set_get_string
27+
if let Ok(res) = r.set("foo", "bar").await {
28+
let res: String = res;
29+
println!("{res}"); // >>> OK
30+
} else {
31+
println!("Error setting foo");
32+
}
33+
34+
match r.get("foo").await {
35+
Ok(res) => {
36+
let res: String = res;
37+
println!("{res}"); // >>> bar
38+
},
39+
Err(e) => {
40+
println!("Error getting foo: {e}");
41+
return;
42+
}
43+
};
44+
// STEP_END
45+
46+
// STEP_START set_get_hash
47+
let hash_fields = [
48+
("model", "Deimos"),
49+
("brand", "Ergonom"),
50+
("type", "Enduro bikes"),
51+
("price", "4972"),
52+
];
53+
54+
if let Ok(res) = r.hset_multiple("bike:1", &hash_fields).await {
55+
let res: String = res;
56+
println!("{res}"); // >>> OK
57+
} else {
58+
println!("Error setting bike:1");
59+
}
60+
61+
match r.hget("bike:1", "model").await {
62+
Ok(res) => {
63+
let res: String = res;
64+
println!("{res}"); // >>> Deimos
65+
},
66+
Err(e) => {
67+
println!("Error getting bike:1 model: {e}");
68+
return;
69+
}
70+
}
71+
72+
match r.hget("bike:1", "price").await {
73+
Ok(res) => {
74+
let res: String = res;
75+
println!("{res}"); // >>> 4972
76+
},
77+
Err(e) => {
78+
println!("Error getting bike:1 price: {e}");
79+
return;
80+
}
81+
}
82+
83+
match r.hgetall("bike:1").await {
84+
Ok(res) => {
85+
let res: Vec<(String, String)> = res;
86+
for (key, value) in res {
87+
println!("{key}: {value}");
88+
}
89+
// >>> model: Deimos
90+
// >>> brand: Ergonom
91+
// >>> type: Enduro bikes
92+
// >>> price: 4972
93+
},
94+
Err(e) => {
95+
println!("Error getting bike:1: {e}");
96+
return;
97+
}
98+
// STEP_END
99+
}
100+
}

0 commit comments

Comments
 (0)