Why exampels from GitHub do not work?

Almost all ... How to study Rust in such situation ? What is going in Crates.io ?

For instance
https://kadekillary.work/post/webscraping-rust/

[deps]
reqwest = "*"
select = "*"
scraper = "*"
prettytable-rs = "*"
extern crate reqwest;
extern crate select;

use select::document::Document;
use select::predicate::Name;

fn main() {
    hacker_news("https://news.ycombinator.com");
}


fn hacker_news(url: &str) {
    let mut resp = reqwest::get(url).unwrap();
    assert!(resp.status().is_success());

    Document::from_read(resp)
        .unwrap()
        .find(Name("a"))
        .filter_map(|n| n.attr("href"))
        .for_each(|x| println!("{}", x));
}

This code is fresh and working fine.. It has been posted 1 day ago

[dependencies]
reqwest     =     "0.11"  
tokio       =   {  version =   "1" ,   features    =   [   "full"  ]  } 
#   [   tokio   ::  main    ]
  pub async  fn main (   )  -> Result   <  (    )   ,   reqwest ::  Error    >  {
    let  my_htm    =   reqwest ::   Client  ::  new (   )
    .get   ( "http://checkip.dyndns.com" )
    .send   (  )
    .await  ?
    .text   (   )
    .await  ? ;

    println !   (   "Current IP Address: 169.255.196.255   {:#?}  "  , my_htm   ) ;

  
    Ok    (   (   ) )  
  
}

It is not clear at all what you are asking. You only state that "this code is fresh and working fine", but this contradicts:

  • your claim that it doesn't work, and
  • the fact that the linked blog post is not "fresh", it is 4 years old (it is from 2018).

Furthermore, you don't describe what exactly the problem is. There are many ways in which a program can fail. Tell us what is or isn't happening. "It doesn't work" won't help us find a solution.

2 Likes

I copy simple codes from GitHub and try compile its and face wrong crates in Crates.io problems.. Almost always.. Is it normal?
First quote is an example of such kind of problem.
Second code is also from GitHub, and it works. Maybe the reason is it is fresh posted . (1 day ago) There is not link to it. I thought it is not necessary

I just study rust, am not searching a solution for my project. My question is what is the best strategy in practicing rust code writing..

It looks like the latest version of the reqwest crate was 0.8.2 when the blog post was created. The current version is 0.11.10; between these versions, reqwest::get() was renamed to reqwest::blocking::get() and placed behind the blocking feature flag. It's generally considered a good idea for authors to specify the dependency versions used, so that users can reproduce their programs later. In this case, the code compiles by setting reqwest = "0.9.0" in Cargo.toml.

8 Likes

...and that's why we should never specify dependency version as = "*", especially for tutorials/examples, though it seems quick and easy way at the time the post is written. It actually harm beginners as our echosystem grow faster.

12 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.