@@ -31,45 +31,26 @@ use std::io::Cursor;
31
31
use ftp :: FtpStream ;
32
32
33
33
fn main () {
34
- let mut ftp_stream = match FtpStream :: connect (" 127.0.0.1" , 21 ) {
35
- Ok (s ) => s ,
36
- Err (e ) => panic! (" {}" , e )
37
- };
38
-
39
- match ftp_stream . login (" username" , " password" ) {
40
- Ok (_ ) => (),
41
- Err (e ) => panic! (" {}" , e )
42
- }
43
-
44
- match ftp_stream . current_dir () {
45
- Ok (dir ) => println! (" {}" , dir ),
46
- Err (e ) => panic! (" {}" , e )
47
- }
48
-
49
- match ftp_stream . change_dir (" test_data" ) {
50
- Ok (_ ) => (),
51
- Err (e ) => panic! (" {}" , e )
52
- }
53
-
54
- // An easy way to retreive a file
55
- let remote_file = match ftp_stream . simple_retr (" ftpext-charter.txt" ) {
56
- Ok (file ) => file ,
57
- Err (e ) => panic! (" {}" , e )
58
- };
59
-
60
- match str :: from_utf8 (& remote_file . into_inner ()) {
61
- Ok (s ) => print! (" {}" , s ),
62
- Err (e ) => panic! (" Error reading file data: {}" , e )
63
- };
64
-
65
- // Store a file
66
- let file_data = format! (" Some awesome file data man!!" );
67
- let reader : & mut Cursor <Vec <u8 >> = & mut Cursor :: new (file_data . into_bytes ());
68
- match ftp_stream . stor (" my_random_file.txt" , reader ) {
69
- Ok (_ ) => (),
70
- Err (e ) => panic! (" {}" , e )
71
- }
72
-
34
+ // Create a connection to an FTP server and authenticate to it.
35
+ let mut ftp_stream = FtpStream :: connect (" 127.0.0.1:21" ). unwrap ();
36
+ let _ = ftp_stream . login (" username" , " password" ). unwrap ();
37
+
38
+ // Get the current directory that the client will be reading from and writing to.
39
+ println! (" Current directory: {}" , ftp_stream . pwd (). unwrap ());
40
+
41
+ // Change into a new directory, relative to the one we are currently in.
42
+ let _ = ftp_stream . cwd (" test_data" ). unwrap ();
43
+
44
+ // Retrieve (GET) a file from the FTP server in the current working directory.
45
+ let remote_file = ftp_stream . simple_retr (" ftpext-charter.txt" ). unwrap ();
46
+ println! (" Read file with contents\ n {}\ n" , str :: from_utf8 (& remote_file . into_inner ()). unwrap ());
47
+
48
+ // Store (PUT) a file from the client to the current working directory of the server.
49
+ let mut reader = Cursor :: new (" Hello from the Rust \ " ftp\ " crate!" . as_bytes ());
50
+ let _ = ftp_stream . put (" greeting.txt" , & mut reader );
51
+ println! (" Successfully wrote greeting.txt" );
52
+
53
+ // Terminate the connection to the server.
73
54
let _ = ftp_stream . quit ();
74
55
}
75
56
0 commit comments