2018-05-22 23:15:08 +02:00
|
|
|
---
|
|
|
|
title: Getting Started
|
|
|
|
menu: docs_basics
|
|
|
|
weight: 130
|
|
|
|
---
|
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
## Installing Rust
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
If you don't have Rust yet, we recommend you use `rustup` to manage your Rust installation. The
|
|
|
|
[official rust guide][rustguide] has a wonderful section on getting started.
|
|
|
|
|
|
|
|
Actix Web currently has a minimum supported Rust version (MSRV) of {{< rust-version "actix-web" >}}.
|
|
|
|
Running `rustup update` will ensure you have the latest and greatest Rust version available. As
|
|
|
|
such, this guide assumes you are running Rust {{< rust-version "actix-web" >}} or later.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
## Hello, world!
|
|
|
|
|
|
|
|
Start by creating a new binary-based Cargo project and changing into the new directory:
|
|
|
|
|
|
|
|
```bash
|
2018-05-26 09:31:13 +02:00
|
|
|
cargo new hello-world
|
2018-05-22 23:15:08 +02:00
|
|
|
cd hello-world
|
|
|
|
```
|
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
Add `actix-web` as a dependency of your project by adding the following to your `Cargo.toml` file.
|
2020-01-03 05:10:24 +01:00
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
```toml
|
2018-05-22 23:15:08 +02:00
|
|
|
[dependencies]
|
|
|
|
actix-web = "{{< actix-version "actix-web" >}}"
|
|
|
|
```
|
|
|
|
|
2020-10-21 11:44:07 +02:00
|
|
|
Request handlers use async functions that accept zero or more parameters. These parameters can be
|
2020-09-12 17:21:54 +02:00
|
|
|
extracted from a request (see `FromRequest` trait) and returns a type that can be converted into an
|
|
|
|
`HttpResponse` (see `Responder` trait):
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
{{< include-example example="getting-started" section="handlers" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
Notice that some of these handlers have routing information attached directly using the built-in
|
|
|
|
macros. These allow you to specify the method and path that the handler should respond to. You will
|
|
|
|
see below how to register the other route that does not use a routing macro.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
Next, create an `App` instance and register the request handlers. Use `App::service` for the
|
|
|
|
handlers using routing macros and `App::route` for manually routed handlers, declaring the a path
|
|
|
|
and method. Finally, the app is started inside an `HttpServer` which will serve incoming requests
|
|
|
|
using your `App` as an "application factory".
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2018-05-23 23:25:51 +02:00
|
|
|
{{< include-example example="getting-started" section="main" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
That's it! Compile and run the program with `cargo run`. The `#[actix_web::main]` macro executes the
|
|
|
|
async main function within the actix runtime. Now you can go to `http://localhost:8080/` or any of
|
|
|
|
the other routes you defined to see the results.
|
2019-08-05 17:29:01 +02:00
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
<!-- LINKS -->
|
2019-06-25 05:36:32 +02:00
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
[rustguide]: https://doc.rust-lang.org/book/ch01-01-installation.html
|
2020-01-02 07:43:41 +01:00
|
|
|
[actix-web-codegen]: https://docs.rs/actix-web-codegen/
|