Skip to content

Commit bbe8692

Browse files
authored
Merge pull request #442 from codecrafters-io/andy/hints
Add Rust and Go hints to stage 2
2 parents 735d053 + d514048 commit bbe8692

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

solutions/go/02-rg2/config.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
hints:
2+
- title_markdown: "How do I access the client's TCP connection?"
3+
body_markdown: |-
4+
This is returned by the [`Accept()`](https://pkg.go.dev/net#Listener.Accept) method.
5+
6+
```go
7+
# The return value value includes:
8+
# 1. the client's connection (a `net.Conn` object)
9+
# 2. an error (if any)
10+
conn, err := listener.Accept()
11+
```
12+
13+
Each [`net.Conn`](https://pkg.go.dev/net#Conn) represents an active connection from a client.
14+
15+
- title_markdown: "How do I send a response to the client?"
16+
body_markdown: |-
17+
Use the [`Write()`](https://pkg.go.dev/net#Conn.Write) method on the client's connection:
18+
19+
```go
20+
# Send the response to the client
21+
conn.Write([]byte("+PONG\r\n"))
22+
```
23+
24+
- The above code uses `[]byte()` to convert the string to a [byte slice](https://go.dev/blog/slices-intro#slices).
25+
- `+PONG\r\n` is the string "PONG" encoded as a [RESP simple string](https://redis.io/docs/latest/develop/reference/protocol-spec/#simple-strings).

solutions/python/02-rg2/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ hints:
1414
1515
- title_markdown: "How do I send a response to the client?"
1616
body_markdown: |-
17-
Use the [`sendall()`](https://docs.python.org/3/library/socket.html#socket.socket.sendall) method on the client's connection for this:
17+
Use the [`sendall()`](https://docs.python.org/3/library/socket.html#socket.socket.sendall) method on the client's connection:
1818
1919
```python
2020
# Send the response to the client
2121
connection.sendall(b"+PONG\r\n")
2222
```
2323
24-
- The above code uses `b` to convert the string to a [bytes object](https://docs.python.org/3/library/stdtypes.html#bytes).
24+
- The above code uses `b` to create a [bytes object](https://docs.python.org/3/library/stdtypes.html#bytes).
2525
- `+PONG\r\n` is the string "PONG" encoded as a [RESP simple string](https://redis.io/docs/latest/develop/reference/protocol-spec/#simple-strings).

solutions/rust/02-rg2/config.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
hints:
2+
- title_markdown: "How do I access the client's TCP connection?"
3+
body_markdown: |-
4+
This is returned by the [`incoming()`](https://doc.rust-lang.org/std/net/struct.TcpListener.html#method.into_incoming) method.
5+
6+
```rust
7+
# The return value is an iterator that provides a sequence of `TcpStream` objects.
8+
for stream in listener.incoming()
9+
```
10+
11+
Each [`TcpStream`](https://doc.rust-lang.org/std/net/struct.TcpStream.html) represents an active connection from a client.
12+
13+
- title_markdown: "How do I send a response to the client?"
14+
body_markdown: |-
15+
Use the [`write_all()`](https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all) method on the client's connection:
16+
17+
```rust
18+
# Send the response to the client
19+
stream.write_all(b"+PONG\r\n").unwrap();
20+
```
21+
22+
- The above code uses `b` to create a [byte string literal](https://doc.rust-lang.org/reference/tokens.html#byte-string-literals).
23+
- `+PONG\r\n` is the string "PONG" encoded as a [RESP simple string](https://redis.io/docs/latest/develop/reference/protocol-spec/#simple-strings).
24+
- To satisfy the Rust compiler, you also need to import the [Write](https://doc.rust-lang.org/std/io/trait.Write.html) trait and mark the TcpStream as [mutable](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html) in order to use `write_all()`.

0 commit comments

Comments
 (0)