nickel-org / nickel.rs

An expressjs inspired web framework for Rust

Home Page:http://nickel-org.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hello World Code Gives Scary Warning!

JimLynchCodes opened this issue · comments

commented

Hi, I was just following along again with the examples here: https://nickel-org.github.io/

I created a new project by:

  • running cargo new hello-kitten
  • adding nickel = "*" to the bottom of Cargo.toml
  • Replacing my main.rs file with the first code snippet on that site:
#[macro_use] extern crate nickel;

use nickel::Nickel;

fn main() {
    let mut server = Nickel::new();

    server.utilize(router! {
        get "**" => |_req, _res| {
            "Hello world!"
        }
    });

    server.listen("127.0.0.1:6767");
}

When I try to run this above code with cargo run it prints a scary warning in the console:

warning: unused `std::result::Result` that must be used
  --> src/main.rs:14:5
   |
14 |     server.listen("127.0.0.1:6767");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_must_use)]` on by default
   = note: this `Result` may be an `Err` variant, which should be handled

And I also see annoying warning squiggles in my code editor:
Screen Shot 2020-08-10 at 10 21 49 AM

Maybe there is some extra compiler setting I can add to remove this?

Possibly we could consider changing the sample code to wrap the "server.listen" line in "Some" which compiles for me with no warnings and seems to work just as well when I call to it:

Some(server.listen("127.0.0.1:6767"));

Good catch, the examples on https://nickel-org.github.io/ have gotten out of date. Everything in the examples fixes this by adding unwrap, i.e. server.listen("127.0.0.1:6767").unwrap();. This is ok for examples and cases where that is the last statement in main. Production uses may want more robust logging of the returned Err(e).

I'll keep this issue open until the https://nickel-org.github.io/ pages have been updated.

commented

Ah, I see. thanks

So am I "losing the errors" when writing it as Some(server.listen("127.0.0.1:6767")); or would it be equivalent in this case server.listen("127.0.0.1:6767").unwrap();?

(or maybe both since returning a plain string can't really fail?)

You could modify your main() function to return a Result<(), Error> and then use server.listen("127.0.0.1:2676")?

Github pages cleaned up