Reading a Cached Copy of Phoronix

At this moment, we are going to focus primarily on the select crate and ignore hyper for now. Access https://www.phoronix.com right now and copy the HTML source into a new file inside the src directory with phoronix.html as the name. We will simply open this file and store it inside of a String to test our program without flooding Phoronix with traffic.

The first function we will write will be a function to open that file and return it as a &'static str' for testing. We can do this by using the include_str!() macro, which takes a filepath as input and returns the contents as a &'static str. Normally a &str would not survive beyond a function call, but because the 'static lifetime was given to the &str it will survive to the end of the program's life.

fn open_testing() -> &'static str {
    include_str!("phoronix.html")
}

When returning values from functions in Rust, the last line of code that is executed which does not have a semicolon will be returned as the result. This eliminates the need to issue a return statement manually. Because the above function is not terminated with a semicolon, it is read as an expression that will return it's value to the caller.

To assign this value to a variable in our calling main() function, we will use the let keyword to create a new variable and assign it's value with the returned value of our open_testing() function.

fn main() {
    let phoronix = open_testing();
    println!("{}", phoronix);
}

This will store the HTML inside the immutable phoronix variable and print that to stdout using the println!() macro. Now run cargo run to try out the changes. If your code is working, it will print the HTML to the terminal via stdout.