12 ms·
Rust for Web
- sjustinas 11y ago"<...> but I don’t understand why I need to write code for both cases, error and success, when I only need value from successful case and I can completely ignore Err or None results." Have you considered `if let`? Seems like the exact thing you need here. https://doc.rust-lang.org/book/if-let.html https://doc.rust-lang.org/book/if-let.html
- EugeneOZ 11y agoOops, it's exactly what I need. Thank you very much, I'll strike out this text in article.
- oconnor663 11y agoThe Result type also has a bunch of methods like `map` and `and_then` that you can use to write code against only the success case. Take a look at https://doc.rust-lang.org/std/result/enum.Result.html https://doc.rust-lang.org/std/result/enum.Result.html
- yawaramin 11y agoSee also https://www.reddit.com/r/rust/comments/3h4u4v/take_advantage_of_iteration_syntax_to_act_on/ https://www.reddit.com/r/rust/comments/3h4u4v/take_advantage...
- CuriousSkeptic 11y agoThat page illustrates an important distinction between expression based languages and procedural ones. You couldn't do that in an expression, as it wouldn't type check without a value for the None case. When I first saw Option it seems like a great way to force handling of the None case, either explicitly in place, or by passing on a mapped option as a return value (monadic composition make this a nice thing). This resembles how exceptions are handled in most procedural languages, either you catch it and handle it, or you don't, and someone else gets to do it further up the call stack. The example on that page looks like the equivalent of an empty catch block. It's almost certainly a bad idea that will bite you in the ass some day.
- tpush 11y agoMaybe I'm misunderstanding you, but why wouldn't it work in an expression based language? Isn't if let pat = expr1 { expr2 } just syntactic sugar for match expr1 { pat => { expr2 } _ => { } meaning the type of expr2 must be unit, and thusly if let is always of unit type?
- CuriousSkeptic 11y agoGranted. But a unit expression is only useful for its side effect so I guess I put that in the gray area between paradigms, not really in the spirit of only evaluating expressions. Which brings us back the point of my comment: It's almost certainly a bad idea.
- sedatk 11y agoThe unhandled case is explicitly mentioned in the latter though.
- masklinn 11y ago> You couldn't do that in an expression, as it wouldn't type check without a value for the None case. Of course you could. If there is no `else` (which there can be, fwiw, just as the else case is optional in a regular if/else expression), the value of the None case is Unit: http://is.gd/Zdgc3D http://is.gd/Zdgc3D
- tatterdemalion 11y agoThis is not accurate. Rust is an expression based language, and `if let`s are expressions. If an `if` or `if let` returns (), it does not need an else.
- masklinn 11y ago> Rust is an expression based language I initially considered writing that, but it depends on what the original poster means by "expression-based language" and they haven't made that clear: in Rust, a function is a sequence of statements. An assignment is a statement for instance, not an expression. To me an expression-based language is something like Haskell or Elm, not only do you have ubiquitous expressions (which Rust does have) there's really no such thing as a statement, a program (or function) simply is an expression (possibly with multiple components) this Rust: let a = 1; let b = 2; a + b is 3 statements. The equivalent Elm or Haskell is a single expression: let a = 1 b = 2 in a + b
- kibwen 11y agoI'm continually delighted to see so many programmers with backgrounds in dynamic languages feeling at home in Rust, given that that's my trajectory as well. :) I'm giving a three-hour Rust tutorial at OSCON this year and I'm honestly torn over whether I should tailor it towards people coming from dynamic langs or people coming from C/C++. I feel like I might end up just having to write two tutorials and take a survey as people come in to determine which to focus on.
- danieldk 11y agoI'm continually delighted to see so many programmers with backgrounds in dynamic languages feeling at home in Rust, It would be nice to get some statistics, because it would be quite unexpected. Still, it seems like some major Rust contributors are coming from the dynamic programming fold. Also, we shouldn't compare Go and Rust, but Go seems to have influx primarily from dynamic languages as well. (I come from a C/C++/Java/Prolog/some Haskell background, dabbling with Rust now.)
- nickpsecurity 11y agoOr you can do one with examples from both worlds for key areas of the language. It would seem that 80+% of each tutorial would be the same anyway. A slight detour into one side shouldn't irritate other part of audience so long as each example isn't too long and you explain it so they can mentally follow the discussion.
- viperscape 11y agoI vote for dynamic. I left C many a year ago for dynamic languages, mostly to avoid C++. Now I'm back with Rust for a while now, thoroughly enjoying it.
- lambda 11y agoI don't necessarily think that it's an either/or situation. I started out with dynamic languages, then did a mix of dynamic languages and C++ for a while (C++ engine with Scheme based scripting language), then went back to dynamic languages, and am now getting into Rust. You are likely to have a mixed audience as well; even if not everyone has that mixed experience like I do, there will probably be a mix of people in the audience. So, it would probably be a good idea to have some examples and descriptions tailored towards those with a C++ background, and some towards those with dynamic language backgrounds.
- nickpsecurity 11y agoGood write-up and experience summary. Nice to see that people from dynamic language backgrounds are able to pick it up.
- shockzzz 11y agoIs it just me, or is using Rust for web apps kinda odd?
- excepttheweasel 11y agoThe main downside to using Rust for web apps - at least in my opinion: is having to learn and use a type system which includes affine types: lifetimes, borrowing etc. But if you've already put in the effort to get to grips with this part of the language, that's not as big a drawback. Then you get all the other positives of the language, like its strong type system, fast speed, sensible package manager, lack of GC pauses etc.
- pcwalton 11y agoI'm in full agreement with this. The learning curve (and the compilation speed I guess--but that's improving quickly) is the main drawback to writing Web apps in Rust. If you've already learned it, though, then it's basically just down to the typical static vs. dynamic typing tradeoff, with Rust just being like any other statically typed language.
- EugeneOZ 11y agoRust's errors handling and memory safety are important differences too.
- losvedir 11y ago> Then you get all the other positives of the language, like [...] lack of GC pauses etc. One thing I've been curious about: is it the case that there are no pauses, or that you know when they happen? I know one of the concerns with GC is it can just kind of happen whenever and pause whatever else you've got going on. It also has to do extra work to track down what sort of memory is reachable or not. Now is rust's RAII-style approach actually faster or do you just have more control over it? It doesn't have to trace reachable memory from the roots like a GC, but it still does have to do some work, right?
- Ciantic 11y agoI've been considering Rust (or Swift) for webapp backend, but these cons are spot on: Named parameters are missing, and one can't build natural looking JSON validation if one can't nest them nicely. Swift looks promising though, if a bit in flux. I've been ramming my head against Scala lately, and while it's nice at times I would like to have compiled language.
- philippnagel 11y agoDid you check out D (http://dlang.org http://dlang.org) yet?
- Ciantic 11y agoNot seriously, maybe I should. I know it has some crazy good macro support if I'm correct, this should allow some nice libraries. Though my first problem with any language is: Does it have type safe SQL builder? Scala has Slick and Quill, and lesser extent JOOQ. Rust has Diesel (not sure how good). It's pointless to start stringly type the database, when pretty much every backend call needs to access it.
- kazagistar 11y agoCould you show an example of what you mean by "natural looking JSON validation"? I am not sure I have seen what you are referring to.
- Ciantic 11y agoI think succint is the word I want. Nested validation logic that looks like the object you are validating. Unfortunately Play docs are not succint, but you may get the idea from here: https://www.playframework.com/documentation/2.3.x/ScalaJsonCombinators https://www.playframework.com/documentation/2.3.x/ScalaJsonC... I tried to implement similar (typesafe) myself using TypeScript: https://github.com/Ciantic/model-validation https://github.com/Ciantic/model-validation there are certain problems I hit with TypeScript, and also I don't want my backend to be JavaScript so I've stopped working on it.
- 11y ago
- squiguy7 11y agoI wish there were more articles like this as I'm genuinely curious of people's experiences using Rust as a backend to their API's or websites. The only example I had known of before this was Rust's own crates.io [1]. [1] https://crates.io https://crates.io
- poorman 11y agohttp://arewewebyet.com/ http://arewewebyet.com/ - (You can use Rust for web stuff, but the ecosystem isn’t mature yet.)
- brobinson 11y agoThis website is quite out of date. It hasn't been updated in 9 months. Related: https://github.com/teepee/arewewebyet/issues/24 https://github.com/teepee/arewewebyet/issues/24
- rubyfan 11y agoI tried Rust with Iron as the article mentions but JSON parsing and writing wacky match expressions turned me off to Rust early on.
- dikaiosune 11y agoOut of curiosity, how long ago was that? I've found that over the last 2-3 months (I started tinkering around 1.4), the ecosystem has already become quite a bit better.
- rubyfan 11y agoAbout 6 months ago I think. It was pre-1.0 and yes some things weren't fully mature. Maybe I'll give it a whirl again soon. I was initially excited about Rust because it appeared to achieve system programming language performance and stability with application programming language expressiveness. The nature of static typed languages doesn't allow for things like first class lazy JSON serde. Java, C, C++, C# all have the same issue. In the grand scheme of things it's not a huge deal if performance is #1 but I value my enjoyment level while coding.
- krullie 11y agoI was wondering how you would test web apps written in rust. In particular the api you're building. Right now as my first rust project I'm trying to implement an HTTP protocol with hyper but I'm not really sure how to test it properly next to just creating a client connection and throwing requests at it. Btw my final idea is to have it as a library that gives you a handler to use with what ever rust framework you choose to use. Not really sure how to do that though, I'm using a lot of hyper stuff.