Skip to content

Commit 64c9a84

Browse files
DOC-5760 started doc support for Rust
1 parent e5767d7 commit 64c9a84

File tree

7 files changed

+186
-5
lines changed

7 files changed

+186
-5
lines changed

build/components/example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
'go': '//',
2727
'c#': '//',
2828
'redisvl': '#',
29-
'php': '//'
29+
'php': '//',
30+
'rust': '//'
3031
}
3132

3233

build/local_examples.py

Lines changed: 4 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'
4042
}
4143

4244

config.toml

Lines changed: 2 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"]
4949
searchService = "/convai/api/search-service"
5050
ratingsService = "/docusight/api/rate"
5151

@@ -67,6 +67,7 @@ rdi_current_version = "1.14.1"
6767
"C#"={quickstartSlug="dotnet"}
6868
"RedisVL"={quickstartSlug="redis-vl"}
6969
"PHP"={quickstartSlug="php"}
70+
"Rust"={quickstartSlug="redis-rs"}
7071

7172
# Markup
7273
[markup]

content/develop/clients/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
Add the `redis` crate as a dependency in your `Cargo.toml` file:
30+
31+
```toml
32+
[dependencies]
33+
redis = "0.32.5"
34+
```
35+
36+
## Connect
37+
38+
Start by importing the `Commands` trait from the `redis` crate:
39+
40+
{{< clients-example set="landing" step="import" lang_filter="Rust" >}}
41+
{{< /clients-example >}}
42+
43+
The following example shows the simplest way to connect to a Redis server:
44+
45+
{{< clients-example set="landing" step="connect" lang_filter="Rust" >}}
46+
{{< /clients-example >}}
47+
48+
After connecting, you can test the connection by storing and retrieving
49+
a simple [string]({{< relref "/develop/data-types/strings" >}}):
50+
51+
{{< clients-example set="landing" step="set_get_string" lang_filter="Rust" >}}
52+
{{< /clients-example >}}
53+
54+
You can also easily store and retrieve a [hash]({{< relref "/develop/data-types/hashes" >}}):
55+
56+
{{< clients-example set="landing" step="set_get_hash" lang_filter="Rust" >}}
57+
{{< /clients-example >}}
58+
59+
## More information
60+
61+
See the [`redis-rs`](https://docs.rs/redis/latest/redis/) documentation
62+
and the [GitHub repository](https://github.com/redis-rs/redis-rs) for more
63+
information and examples.

data/components/redis-rs.json

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

0 commit comments

Comments
 (0)