summaryrefslogtreecommitdiffstats
path: root/examples/hello_world.rs
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2019-10-22 10:13:49 -0700
committerGitHub <noreply@github.com>2019-10-22 10:13:49 -0700
commitcfc15617a5247ea780c32c85b7134b88b6de5845 (patch)
treeef0a46c61c51505a60f386c9760acac9d1f9b7b1 /examples/hello_world.rs
parentb8cee1a60ad99ef28ec494ae4230e2ef4399fcf9 (diff)
codec: move into tokio-util (#1675)
Related to #1318, Tokio APIs that are "less stable" are moved into a new `tokio-util` crate. This crate will mirror `tokio` and provide additional APIs that may require a greater rate of breaking changes. As examples require `tokio-util`, they are moved into a separate crate (`examples`). This has the added advantage of being able to avoid example only dependencies in the `tokio` crate.
Diffstat (limited to 'examples/hello_world.rs')
-rw-r--r--examples/hello_world.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/examples/hello_world.rs b/examples/hello_world.rs
new file mode 100644
index 00000000..8ff40902
--- /dev/null
+++ b/examples/hello_world.rs
@@ -0,0 +1,33 @@
+//! Hello world server.
+//!
+//! A simple client that opens a TCP stream, writes "hello world\n", and closes
+//! the connection.
+//!
+//! You can test this out by running:
+//!
+//! ncat -l 6142
+//!
+//! And then in another terminal run:
+//!
+//! cargo run --example hello_world
+
+#![warn(rust_2018_idioms)]
+
+use tokio::io::AsyncWriteExt;
+use tokio::net::TcpStream;
+
+use std::error::Error;
+
+#[tokio::main]
+pub async fn main() -> Result<(), Box<dyn Error>> {
+ // Open a TCP stream to the socket address.
+ //
+ // Note that this is the Tokio TcpStream, which is fully async.
+ let mut stream = TcpStream::connect("127.0.0.1:6142").await?;
+ println!("created stream");
+
+ let result = stream.write(b"hello world\n").await;
+ println!("wrote to stream; success={:?}", result.is_ok());
+
+ Ok(())
+}