4 ms·
I can assure you that Rust's syntax is not designed to further separate application development from system development, if anything, we encourage a way broader
by steveklabnik 7y ago
I can assure you that Rust's syntax is not designed to further separate application development from system development, if anything, we encourage a way broader audience to learn Rust than just pure systems programmer folks. In general, Rust's syntax is intended to be vaguely similar to C/C++/Java, but with some influence from functional languages where appropriate, and with syntactic forms that work better with type inference. There is very few syntactic forms that are truly novel in Rust (even lifetimes are taken from OCaml's generic syntax, because lifetimes are generics!)
That said,
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
would be something like
[[ noreturn ]] void my_panic(&core::panic::PanicInfo _info) {
in C++, which is very similar, syntactically speaking. The main differences are:
* fn for function declarations
* Rust has the return type after parameter list, and adds ->
* ! is [[ noreturn ]], less punctuation actually!
* no need for the superflous void return type
* "name: type" not "type name", which adds a single :
I am admittedly very biased, but it seems much more similar than different.
- gpm 7y agoIn addition to this excellent reply, if you were using the type core::panic::PanicInfo commonly (i.e. "twice") you would import PanicInfo at the top of the file use core::panic::PanicInfo and the function would become the much shorter fn my_panic(_info: PanicInfo) -> ! {
- steveklabnik 7y agoYes, and you can do something similar with C++ too; I debated if I should include it or not for this reason. (The semantics are a bit different...) Thanks for bringing it up!
- petschge 7y ago! might be "less" punctuation than [[ noreturn ]] but is a lot harder to google. And why do you need the "->"? That is just more line noise.
- gpm 7y agoBecause `fn foo() u8 {}` doesn't read as nicely as `fn foo() -> u8 {}`? Also because "no return type" is the most common return type, and this way representing "no return type" as omitting the `-> ()` reads nicely: `fn foo() {}`
- lone_haxx0r 7y agoIn my opinion, "u8 foo() {}" looks and reads better than both of those.
- gen220 7y agoI’d agree that it looks better in isolation. I think that the advantage of leading with `fn` arrives when you have paragraphs of functions to scan through. Leading with a consistent word (be it fn, func, or whatever) immediately and disambiguously identifies the line as defining a function (rather than a variable, or whatever), which is a useful landmark to anchor on or pivot from.
- umanwizard 7y agoSo, using grep or a similar tool, how would you find the definition of the function, separately from its uses? In a Rust codebase I could just run `grep -r "fn foo" .` and find the definition immediately. Clearly this doesn’t matter if you’re in an IDE with a working “find definition” feature, but that’s often enough not the case that I find easily greppable definitions to be a gigantic advantage.
- jcelerier 7y agocome on, C++ IDEs had "go to definition" features for literal decades
- umanwizard 7y agoIn what way does this contradict my comment?
- klyrs 7y ago> "name: type" not "type name", which adds a single : Sorry for asking a newbie question that's a quick search away, but is there an equivalent to "type name, name, name..."? (I'm biased towards C myself but contemplating a switch...)
- steveklabnik 7y agoThe grammar is let PATTERN: TYPE = Rust’s pattern syntax is richer than just introducing variables. I’m on my phone, so it’s a bit hard to show off all of the things that can be done. In this exact case, though, it usually looks like let (x, y, z) = or with types, let (x, y, z): (TYPE, TYPE, TYPE) =
- klyrs 7y agoYeah, I can live with that.
- somebodynew 7y agoIn C++ unary prefix & is used in an expression to take the address of an object. To declare a parameter of reference type the & would be between the type name and the parameter name (i.e. after PanicInfo/before _info).
- steveklabnik 7y agoGuh, yes, thank you. (It's been a long, long time since I wrote significant amounts of C++.)