-1

i want my hello world to display hello, {} but everything i do not working and i have absolutely no idea how to solve the errors i get here is my code:


#[macro_use] extern crate rocket;
use std::fmt::format;

#[get("/")]
fn index() -> &str
{
    hello("curlynux")
}

fn hello(blaze: str) ->  str
{
    return format!("hello, {} !", blaze);
}

fn main()
{
    rocket::ignite().mount("/", routes![index]).launch();
}

and the error log:

warning: unused import: `std::fmt::format`
 --> src/main.rs:4:5
  |
4 | use std::fmt::format;
  |     ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0106]: missing lifetime specifier
 --> src/main.rs:7:15
  |
7 | fn index() -> &str
  |               ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
  |
7 | fn index() -> &'static str
  |               ~~~~~~~~

For more information about this error, try `rustc --explain E0106`.
warning: `test_project` (bin "test_project") generated 1 warning
error: could not compile `test_project` due to previous error; 1 warning emitted
curlynux
  • 1
  • 3

1 Answers1

0

The error message says everything about the problem. Read some Rust basics if you don't understand it.

You have to add your attribute macros. I removed them for testing with the rust playground.

fn index() -> String {
    hello("curlynux")
}

fn hello(blaze: &str) -> String {
    format!("hello, {} !", blaze)
}

fn main() {
    println!("{}", index());
}

Aitch
  • 1,617
  • 13
  • 24
  • your code do not work it's returning this error: `expected struct 'std::string::String', found '()'` – curlynux Aug 07 '22 at 15:37
  • you must have changed something, it works: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f6b93a575a1cb711c3cce19b7a1852c7 – Aitch Aug 07 '22 at 18:30
  • i have not changed anything i literally copy past your code ... but lemme try again, how to print it in html response to display it in browser with rocket ? – curlynux Aug 07 '22 at 22:57