File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -66,9 +66,9 @@ async fn get() -> io::Result<Vec<u8>> {
6666 let mut buf = vec! [];
6767
6868 io :: timeout (Duration :: from_secs (5 ), async {
69- stream . read_to_end (& mut buf ). await ?
69+ stream . read_to_end (& mut buf ). await ? ;
7070 Ok (buf )
71- })
71+ }). await
7272}
7373
7474fn main () {
Original file line number Diff line number Diff line change 1+ use std:: time:: Duration ;
2+
3+ use async_std:: {
4+ prelude:: * ,
5+ task,
6+ io,
7+ net:: TcpStream ,
8+ } ;
9+
10+ async fn get ( ) -> io:: Result < Vec < u8 > > {
11+ let mut stream = TcpStream :: connect ( "example.com:80" ) . await ?;
12+ stream. write_all ( b"GET /index.html HTTP/1.0\r \n \r \n " ) . await ?;
13+
14+ let mut buf = vec ! [ ] ;
15+
16+ io:: timeout ( Duration :: from_secs ( 5 ) , async {
17+ stream. read_to_end ( & mut buf) . await ?;
18+ Ok ( buf)
19+ } ) . await
20+ }
21+
22+ fn main ( ) {
23+ task:: block_on ( async {
24+ let raw_response = get ( ) . await . expect ( "request" ) ;
25+ let response = String :: from_utf8 ( raw_response)
26+ . expect ( "utf8 conversion" ) ;
27+ println ! ( "received: {}" , response) ;
28+ } ) ;
29+ }
You can’t perform that action at this time.
0 commit comments