Follow the steps to implement this recipe:
- Open the main.rs file in the src directory in your preferred text editor.
- Write the code header with the relevant information:
//-- #########################
//-- Task: Url experiments
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 04 May 17
//-- #########################
- Create the error_chain! macro to define a custom Error and Result type, along with automatic conversions from the standard library error types, after the code header:
#[macro_use]
extern crate error_chain;
error_chain! {
foreign_links {
UrlParse(url::ParseError);
}
}
- Define the run method and the quick_main! macro by copying and pasting the following code snippet with the error_chain! macro:
extern crate url;
use url::Url;
fn run() -> Result<()> {
let s = "https://github.com/rust-lang/rust/issues?labels=E-easy&state=open";
let parsed = Url::parse(s)?;
println!("The path part of the URL is: {}", parsed.path());
Ok(())
}
quick_main!(run);
- Save the file and run the project by running the following command:
cargo run
We should get the following output on execution of the code: