3 ms·
There is obviously a learning curve but it's hard to argue that: #[get("/hello/<name>/<age>")] fn hello(name: String, age: u8) -> String { format!("Hello
by qaq 7y ago
There is obviously a learning curve but it's hard to argue that:
#[get("/hello/<name>/<age>")]
fn hello(name: String, age: u8) -> String {
format!("Hello, {} year old named {}!", age, name)
}
is more complex than express + Node. With added bonus that you get validation for free
- tinco 7y agoI'm a firm believer in that Rust is the greatest imperative programming language ever designed. But your example is a pathologically simple problem with a horrendously complex solution in Rust. In responding to a simple get request you have to lean on both function annotations and a macro. That code would be so much nicer in Ruby.
- qaq 7y agoWould you post an equivalent code in Ruby ? (that like above does validation of inputs)
- johnlinvc 7y agoNot exactly the same, since there's no UInt8 in Ruby. require 'sinatra' get %r{/hello/([^/]+)/(\d+)} do |name, age| "Hello, #{age} year old named #{name}!" end
- tinco 7y agoIf you need input validation like that, which you normally wouldn't in Ruby because your models should do it for you, I'd probably use grape or something similar: require 'grape' params do requires :age, type: Integer requires :name, type: String end get "/hello/:name/:age" do "Hello, #{params[:age]} year old named #{params[:name]}!" end In any case, my beef is more with the macro than with the function annotation, which is rather spiffy. The macro hides the fact that string manipulation in Rust definitely requires a little bit of manual reading. And I feel that as soon as your app becomes more complex Rust will just start getting more in the way. And to an experienced Rust dev that might not be a big deal, because an experienced dev knows how to work with strings or which memory management strategy so it won't bog them down. But if you're just working on something and you quickly want to whip out a service that tells people what age they are, I'd definitely go for a quick Ruby or Go service.
- steveklabnik 7y agoOne of the things this API can do for you is not give you just strings, but fully typed instances of structs. And you don’t need to do the conversions yourself. So you’re not wrong, but it’s easier than you may imagine.