rustRust Web server example
Rocket is a web framework for Rust that makes it simple to write fast web applications without sacrificing flexibility or type safety.
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
Output example
Hello, world!
The code above is an example of a simple web server written in Rust using the Rocket framework. It defines a function called index
that is triggered when a GET request is sent to the root path of the server. The function returns a string literal containing the text "Hello, world!" which is then sent back to the client.
Code parts:
#[get("/")]
- This is an attribute macro that tells Rocket to call theindex
function when a GET request is sent to the root path of the server.fn index() -> &'static str
- This is the definition of theindex
function. It takes no arguments and returns a string literal."Hello, world!"
- This is the string literal that is returned by theindex
function.
Helpful links
More of Rust
- How to match whitespace with a regex in Rust?
- How to split a string with Rust regex?
- How to use regex to match a double quote in Rust?
- How to use non-capturing groups in Rust regex?
- How to use regex with bytes in Rust?
- How to replace a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to replace all matches using Rust regex?
- How to use regex to match a group in Rust?
- How to get a capture group using Rust regex?
See more codes...