As posted in Rust Framework Rocket Goes Stable, Rocket will be compatible with Rust Stable, starting with version 0.5. It is almost September, and we will have the new release any time soon. We can already use the latest Rocket codes from the master branch for non-production codes for those who cannot contain their excitement. This post shows how to use the newest Rocket web framework development codes from GitHub using Rust stable.
Ensure We Are Using Atleast Rust Stable 1.45.0
Please run the following command on the command line to check the version of our Rust stable.
1 | rustc --version |
For this post, we are using Rust 1.46.0.
Create A Rust Project
This post uses IntelliJ IDEA and an IntelliJ Rust plugin to hasten the creation of a Rust project. Alternatively, we can use the Cargo CLI tools.
Modify Cargo.toml To Use Rocket 0.5 (Latest Development Codes)
Then, modify the Cargo.toml file, as shown below.
1 2 3 4 5 6 7 8 | [package] name = "rust-rocket-starter" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" [dependencies] rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "master" } |
In the file, we indicated a dependency on the Rocket framework codes (not a crate) in GitHub. The git property refers to the Git repository URL, while the branch property refers to the branch we will use.
As we can see, the latest release is still Rocket 0.4.5. Therefore, we do not have a Rocket 0.5 crate yet.
Modify The main.rs To Use Rocket 0.5 Compatible Codes
The following codes are sample codes from the Rocket GitHub repository URL.
1 2 3 4 5 6 7 8 9 10 11 | #[macro_use] extern crate rocket; #[get("/<name>/<age>")] fn hello(name: &str, age: u8) -> String { format!("Hello, {} year old named {}!", age, name) } #[launch] fn rocket() -> _ { rocket::build().mount("/hello", routes![hello]) } |
If we use codes example from Rocket 0.4.5 (via crates.io), they will not compile.
Test Rust Stable And Rocket 0.5 (Latest Development Codes)
To test our codes, run the main.rs file within the IDE. We get the following output on the console.
Then, open and web browser and try the following URL – http://localhost:8000/hello/karl/24. We get to the next page.
Since Rocket 0.5 release is not yet available officially, there may still be changes to the codebase. It is best to postpone any plans of migrating codes that rely on Rocket 0.4.5 and Rust nightly until we get the 0.5 release version.
This post is part of the Rust Programming Language For Beginners Tutorial.
The Rocket framework 0.5 is now a release candidate (0.5-rc). Almost there! Let’s wait a little more.