Curl::easy, unable to understand how it works


#1

Hi all, i’m new to rust, and i’m not very skilled with it’s features.
I’ve tried to use curl::Easy, but i’m unable to understand what this code do.
The original code from tutorial ;

    extern crate curl;
    use curl::easy::Easy;

// Capture output into a local `Vec`.
fn main() {
    let mut dst = Vec::new();
    let mut easy = Easy::new();
    easy.url("https://www.rust-lang.org/").unwrap();
    let mut transfer = easy.transfer();
    transfer.write_function(|data| {
        dst.extend_from_slice(data);
        Ok(data.len())
    }).unwrap();
    transfer.perform().unwrap();
} 

I’ve modified the code in this way ;

extern crate curl;
use curl::easy::Easy;

// Capture output into a local `Vec`.
fn main() {
    let mut dst = Vec::new();
    let mut easy = Easy::new();
    {
    easy.url("https://www.rust-lang.org/").unwrap();
    let mut transfer = easy.transfer();
    transfer.write_function(|data| {
        dst.extend_from_slice(data);
        Ok(data.len())
    }).unwrap();
    transfer.perform().unwrap();
    }
    println!("{:?}", dst);
         
    }

But the output it’s only numeric, i don’t understand why i cannot see the webpage splitted into element of the vector.
thanks a lot.
G.


#2

I’ve not used the curl crate, but it looks like it just hands you the raw bytes of the response; those bytes are written to the Vec. You can try std::str::from_utf8(&dst) if you’d like to materialize a string slice from the collected bytes.