Why not?
With Node or Go, this is trivial.
Why not?
With Node or Go, this is trivial.
The HTTP Handler (the index function) returns some plaintext and prints to the server's console. Removing the print makes the function simpler than the one in the main article.
Actix Web was designed from the get-go to be a highly concurrent HTTP framework, and I think it is the most mature Rust web framework.
Of course, the boilerplate main still needs to be written, and the way it does things is a bit more profound. But at a high level, I believe it is quite clear that it is handling logging, URL routing and connecting to a port.
extern crate regex; // 1.1.8
fn main() {
let pattern = regex::Regex::new("(?i)resource not found").unwrap();
let msg = "resource not found".to_string(); // didn't have to convert to String as &str suffices
if pattern.is_match(&msg) {
println!("found pattern in msg");
} else {
println!("did not find pattern in msg");
}
}Clearly, this is an easy example and when used in the wild, you'll have to account for error handling. However, whatever isn't trivial exists for a good reason. Trivial comes at a cost. One thing I like about Rust is that it lets a programmer decide what costs to assume.
let msg = "resource not found".to_string();
Is it necessary?> Is it necessary?
In this code, it's not actually necessary, your parent converts it back to a &str later, when passing it to is_match.
In other cases, it's very necessary; they're two different types, with different properties.
Rust has a few "rules of three" that appear again and again. This is an instance of there being three kinds of types: T, &T, and &mut T. That is, owned, borrowed, and mutably borrowed. In most examples of this, "T" is the same thing in all places, that is, i32, &i32, &mut i32, but in this case, "str" is built into the language. So you have String, &str, and &mut str. The naming is slightly off, because you're coming into the conflict of String being a standard library type, and str being a language type. We almost unified them before 1.0, but there wasn't a ton of benefit.
The point is, yes, it is a bit confusing at first, you're totally right. I actually re-did the entire table of contents of The Rust Programming Language to re-focus them on strings due to this. But you're also right that after some practice, it's a non-issue that you never really spend mental effort on.
The trick is, how many people will be willing to spend that time? Can we reduce that time? Time will tell :)