296 points · by xurukefi · 6y ago · 275 comments
Honestly, I like it, and for all the non-hyped reasons.
Like ownership. Lots of us in the embedded space aren't so impressed by the extra memory safety because, if you allocate all your memory statically, there are fewer ways to screw things up with references, and it's a lot easier to do bounds checking, so fewer bugs slip in. Not that "even fewer" isn't good, it's just that the impact is less dramatic than in application code.
But: the borrow checker still pesters me about something I've written every once in a while. And every once in a while, what it pesters me about is a data race that would have definitely bit me in the back sooner or later. It has no security implications, I guess, and it wouldn't crash my program, but it's still wrong. Yay!
Or like traits, which are just so refreshingly sane.
Lots of people are hung up on security and segfaults, and spend far less time talking about a far less categorical property: the conventions that Rust enforces make it easier to write correct code. Sometimes it's not straightforward code, mind you, but "works right" trumps "fits in a single line" in my book...
In many ways, Rust is what C++ could have been if it had not been designed by saying yes to everything. I guess it's not there yet, but there's a chance it might be.
On a more, uh, philosophical note, I think it's pretty cool that our industry has been granted a second chance in this regard. We've already been given Ada, and we missed the chance to adopt it on a wide enough scale to make a difference. Maybe we won't miss this chance.
From mostly an outsiders' perspective, I just don't think C is a very nice programming language. It looks easy to to pick up and it probably is (for some definitions of "pick up"), but I don't think that's a good thing. The language gives you almost nothing. Pointers, structs, arrays, control flow. Not much else. I'm barely exaggerating when I say that all the C code I see looks like a chaotic pointer juggle fest for precisely that reason. The few features and library that it does have come with non-obvious gotchas that will punish you later.
Rust is probably much harder to "pick up", and way more complicated in general, but it's just, uh, more language. I'll take pattern matching, encouraging immutability, functional style programming, trait programming over C's simplicity any day of the week. The safety is extremely nice, but I think Rust's biggest eye-opener for me is that all these "higher level" features are just possible at (close to) the performance of C. That makes it very appealing to me.
I think that's part of the problem :) Because Rust looks high level it appeals to people used to high-level programming. But once you get into systems programming, the issues are different from high-level programming, and you're less happy to pay for features that solve other problems. What I'm saying is, obviously, a matter of taste, and some people doing low-level programming do prefer C++'s high-level feel, which is also found in Rust. I hope it goes without saying that just programming in Rust -- or C++ -- doesn't mean you're doing systems programming unless you're, say, writing a device driver, a kernel, a GC, a game framework etc.
I think one of the previous comments, and others on other forums have things right when they say that Rust is a better C++. Anything you would consider using C++ for is a good candidate for using Rust for. Rust is not a better C though, it's just a bit too high level for that. There are languages that are aiming to be a better C (Zig is REALLY interesting, but very very new right now), but Rust isn't really one of them.
I could very easily see something like an OS aimed at x86 desktops being written in Rust. Desktop systems are beefy enough and the problem space is complex enough that the extra overhead involved in Rust (similar to C++) makes sense. On the other hand if you're doing some bare metal work on something like an Arduino you just don't have enough there to justify the extra overhead and something very basic like C (or assembly, or Zig once it matures a bit more) is probably a better fit.
If you're doing anything higher level than kernel programming, Rust is just as viable as anything else, at that point it's mostly down to personal preference on whether you can deal with not having conveniences like garbage collection or not.
Now, for those who get involved enough in it, they might find issues with the language, and that will provide them incentive to get comfortable with C. Or maybe they won't; maybe they'll get turned off at that point, or maybe they'll find making Rust work a better option, or they'll find still another language that is even closer to C, without the holes.
But either way, providing a way to move in that direction without having to immediately accept all the baggage C brings seems like a good thing. It certainly has moved my mindset from "screw embedded unless it's beefy enough I can run Erlang" to "well, maybe; Rust looks interesting". And I figure if I get comfortable enough with looking into the space, I either
A tool that gets more decent developers to even look at embedded sounds like it would have been a good thing to have 5+ years ago, but better late than never.
If you want higher level abstraction only in some parts of your codebase, you can do that too. It's easy to mix-and-match.
Although they do have security issues regarding use-after-free.
1. Rust is surprisingly great for making cross-platform CLI tools and shipping static Linux binaries. Everybody can just install a single executable and they're ready to go. (Go is good at this, too.)
2. Rust is great for anything where I ask, "How big a cluster do we need for this?" I get roughly the same speed as C++, except there are far fewer footguns. And I can add threading support to existing code in an afternoon, and I can expect it to just work.
3. Rust's very new async support is powerful in some very interesting ways, and I have a couple of programs that it has worked beautifully for. (I'm working on an upcoming open source release of one of them.)
4. Rust's algebraic data types (aka "enum" and checked "match") are a joy for any software involving lots of special cases and complex logic. I think every typed, high-level language should steal this feature.
However, I have mixed experience using Rust for business logic. Yes, item (4) above does help with business logic. But business logic changes a lot, and it needs to be accessible to many developers within an organization.
So on my recent team projects, Rust often winds up doing the heavily lifting. But the rapidly-changing business logic will often get deferred to a GCed language. So far, this is working nicely.
The iterators, enums and no Null types lend themselves to interesting solutions.
The crate ecosystem is also developing fast, and there are decent browsable docs and bootstrapping the env and development environment are all easy.
SDL2 support makes it really easy to make rectangle based games like classic pong and experiment with keyboard and controller inputs etc. (never mind further possibilities of 3D etc).
TTY apps and games are also easy to make with the termion crate.
I think it's just kind of worth learning in a way that (for example) Python, C and Haskell are complementary options for learning different ways of thinking about of coding. Rust feels like a different flavour of programming. It's borrow checker and branch checking etc. they really are likely to lead to better programming in other languages too. Understanding ownership and considering the transfer of ownership are valuable.
Rust is without a doubt a (much) "better C++" but I'm not sure a better C++ is necessarily what systems programming needs most.
[1]: Not that writing that code is as easy as in a high-level language, nor does it offer the same level of abstraction as high-level languages, where changing the details of the implementation does not affect the API and its clients, but it looks high level, or "expressive", on the page.
Is there a reason you’d write your own Rust compiler frontend, rather than just letting rustc generate LLVM IR as it does, and then writing your own LLVM arch target? None of the Rust complexity leaks over into the LLVM IR it generates; it looks just like the LLVM IR that e.g. clang generates.
The leakage even triggers performance issues in compilers: https://users.rust-lang.org/t/5-hours-to-compile-macro-what-...
As an embedded consultant, a better C++ is what systems programming needs most not for technical reasons, but social. So many embedded shops are embracing C++ and it's causing all the sorts of problems you might imagine. While it's possible that these shops will tack back towards a lower-level language, my prediction is that would be a much harder sell than something like rust.
Now, note that I am not saying that Zig's approach to safety is definitely better than Rust. Correctness is an extremely complex issue, with many factors, and it's hard to say with any certainty what works and why. My preference for Zig (which is largely hypothetical as Zig doesn't really exist in a meaningful sense yet) is because I personally value language simplicity and short cycles over what some call "expressivity", but as someone who is also very much interested in formal methods and software correctness in general, I am not convinced that Rust's complexity buys you increased correctness over Zig, and it may even be worse. So the tradeoff is simplicity vs. "expressivity", where correctness can go either way.
I agree but if better C++ is what the PL industry can give me, then I'll take it, thank you very much!
(Frankly at this point I'll take anything that isn't C++...)
I think this needs to be more specific. Comparing Rust and C, for instance, it seems complexity can be used to improve correctness; the tradeoff is worth it at least some of the time.
It seems to me the best example of a simple correctness-oriented language isn't Zig, it's Ada SPARK, which has been used in various life-critical software systems. [0] SPARK's advantages are due to its simplicity:
* The whole language has been defined formally (zero ambiguity)
* The language is amenable to formal verification
A monstrously complex language like C++ brings many features, but is much harder to reason about formally, and will never have a formal definition.
[0] https://en.wikipedia.org/wiki/SPARK_(programming_language)
Huh? They are apples and oranges.
Is Rust intended to provide "Direct mappings of built-in operations and types to hardware to provide efficient memory use and efficient low-level operations"? That's one of C++' design goals.
If you change the design goals, you get different advantages and disadvantages.
See more about the C++ design goals in this interview:
https://www.electronicdesign.com/technologies/dev-tools/arti...
I'm curious what your reasoning is for saying this?
1. A lot of resource-constrained firmware is so straightforward that Rust's benefits simply don't justify the effort of learning it and using it. In turn, without sufficient demand, there isn't much motivation for large tooling vendors to get into the game. Without the large tooling vendors, the kind of companies whose money could shift the landscape significantly won't get in the game, either.
2. Lots of embedded software development is outsourced. The margins are pretty small, and the companies in this field are both unwilling and unable to build technical expertise early on, so there's a lot of inertia.
3. Lots of embedded software is developed based on manufacturer-supplied SDKs. Manufacturers in this field tend to run high-inertia, cost-conscious software teams that take a long time to catch up with the rest of the software industry.
4. A lot of this inertia is mistaken for prudence ("we value stability", "we don't just jump on the hype train") and touted as a positive thing. That's why IoT security is what it is, for example (and it's not just IoT, and not just security. Lots of areas in the embedded space suffer from terrible practices). This is an industry where an embarrassingly large number of companies are just discovering continuous integration. A whole new programming language won't just happen overnight.
5. Lots of resource-constrained firmware is written with pretty strenuous regulatory constraints in place, under considerable pressure from internal standards, audits and so on. In some regards, Rust's tooling ecosystem still needs to grow and mature. AFAIK there's no serious equivalent to Coverity, for example -- and plenty of companies value it not just for the analysis it makes (which doesn't mean as much as it seems because the Rust compiler does a lot of static analysis on its own), but also for the kind of traceability and integration that it gives them, and the ability to implement the kind of internal processes that look good on paper during an audit.
It's a mix of legitimate and non-legitimate concerns (specifically, #5 is very legitimate -- lots of internal processes are just software safety theater, but many aren't).
Do you think that Rust could instead help outsourcing? Similar to how Java's GC keeps teams away from segfaults, could Rust's safety guarantees give companies more confidence that an outsourced team's code would have fewer critical bugs?
There is a massive number of devices between an 8-bit PIC and a Cortex-A. Obviously you're not going to use Rust on a tiny PIC or similar, hell that architecture is outwardly hostile towards even C. I'm guessing these very low power chips will exist for many years to come, but a lot of work has moved over to things like Cortex-Ms which absolutely benefit from things like Rust.
ASM is for the use-case where you're optimizing code in terms of using specific instructions or intrinsics; but it's not really for the case where you're concerned with specific opcode encodings (as ASM abstracts that away, grouping multiple different opcode encodings—some short, some very long—behind a single friendly instruction name.) In that case, you have to work with the machine-code directly—or, at least, through data structures that contain machine-code at the base level, and so can tell you exactly what machine-code they contain.
These systems have the resources to run managed runtimes. I’ve shipped embedded software running .NET core 2.2 on ARM Linux (RK3288 with custom set of peripherals), only a minor part of the software being unsafe (a shared library written in C++).
Over the last 20 years, desktop GUI software, web servers, and mobile software migrated from C++ towards managed languages, and they never looked back. I’m pretty sure the majority of embedded software gonna follow the same path.
I also like some of the abstractions. They're clever in their simplicity. For example, an iterator over an array or vector is simply doing a for loop using pointers.
Rust goes all out on eliminating undefined behavior, and may well be the best way of doing that in low-level programming, but eliminating UB is just a means to an end, UB is not the only source of dangerous bugs, and if the cost of eliminating it is high it may not be the best way overall of reducing bugs in low-level languages.
It's worth pointing out that Rust combats more than undefined behavior. Undefined behavior is a category of bad things that Rust works to eliminate, but Ownership goes beyond eliminating UB, and eliminates a large swath of potential logic bugs.
Most people that starts using Rust get stuck on issues that you woudn't have with other languages.
What tools are these, and why haven't they been deployed across every C codebase to prevent memory safety bugs?
For example match statements need to be exhaustive. Your program won't compile unless they are.
With Rust the compiler is already providing a lots of analysis, and you can't easily skip it. And if you do by using unsafe{}, it shows. So basically, even if your dependencies are provided by the worst team in the world, you know that the guarantees enforced by the Rust compiler are in the code. (To be fair, it does not mean that the SW will do anything useful, but it won't likely segfault)
I believe the language server is just for real time editor integration.
This is basically what all type checking does. It's not specific to Rust.
The borrow checker is unique to Rust. So is compiler enforcement of only allowing thread safe data structures to cross thread boundaries.
Both of those are a pretty big deal.
> % of developers who are developing with the language or technology and have expressed interest in continuing to develop with it
Because there aren't that many Rust jobs, almost no one has to use it. Which means people either try it and leave, they like the language and stick around, or they never use it (the overwhelming majority of developers). This leads to the Rust being the most loved language by the above definition, which is completely different from "popular" (as in the title of the OP).
If I make up my own language that I plan to never stop using, even if no one else ever touches it, I'll get 100% "Most Loved" on StackOverflow's survey.
As you said, mainstream languages won't win this one. But it is a valid metric anyway, and there is plenty of merit on winning it.
But most of the time, people can't stop using languages.
But it's not the answer of everything, to me it's nowadays abused in a lot of projects, there are instances of project where using Rust not only doesn't make a lot of sense but also can be slower than another languages, there are cases where Go is better, other where Node.js is better, other where Python is better, other where Java is better, etc. To me the Rust community seems to want to argue that everything should be rewritten in Rust, just because Rust solves every problem, while it's not.
> Lots of these things that look like syntax problems are in fact design problems surfaced by the compiler.
A lot of people are finding value in the rules Rust provides. This is irrespective of domain. The strict compiler helps you refactor code in ways that less strict languages don't. You still can do it, of course, you just have a lot less help.
The trick is, is the initial hit in productivity worth it, until you get up to speed?
That said, of course everything doesn't need to be in Rust. But there's a reason it's finding broader appeal than purely systems stuff.
That said, Rust’s domain does seem wider than just systems programming. It has the right abstractions to be suitable for (edit: some) higher level things like web apps.
Personally Rust is turning into what I was assuming and hoping Go was going to turn into back in 2010: a static language that does most of what we all used dynamic languages for, without too much extra difficulty. (That is, after you get used to the borrow checker’s rules.)
Frankly any language that makes me write "some string".into(), and all of the other nonsense of Rust, is not suitable for writing string processing applications (as opposed to utilities, such as ripgrep). The business logic of any Rust application that deals mostly with strings is hopelessly obtuse and/or obscured. Languages like Perl, Python and Ruby, or even JavaScript and Java, are more suitable.
Rust focuses on and succeeds at being an improved C++, no reason to use it for everything.
Do you have any hypotheses about why we don't see as much publicity for Haskell then?
Of course, Rust gives you a lot in exchange for the reduced velocity--safety and performance; however, those are tradeoffs and we should talk about them as such. It feels dishonest to present Rust as "as productive as Python" even with the "(given enough experience)" caveat.
It really depends on how you measure productivity.
But also, I don’t think that “makes you think about memory” is that simple. I don’t have to think about memory in Rust very often at all: that’s what the compiler is for! When I make a mistake, it corrects me, and I’ve only thought about it for a few moments.
I haven't seen this. At most, I've seen the push for rewriting important libraries where security is a core issue in Rust. I don't blame them. I'm tired of C/C++ being used everywhere because of performance, leading to the current-day situation of so much fundamentally unsafe code running the world.
As in C or C++. I didn't mean it as its own language. And C++ has plenty of issues where I'd consider it equally unsafe. Unless you're a god tier programmer, and even then, it's impossible to write safe code without insane amounts of effort that can't be expected in most projects.
I don't know about node. But there's whole lot of java code and libraries out there. Otherwise why would people build other languages for the JVM?
Ease of writing; Most of code is okay having a GC. But time to produce working code (not necessarily 100% correct code) is important.
Rust is created for low level stuff. It is wrong to expect web backend programmers to deal with ownership, stack-vs-heap and all.
I am a web backend programmer. I respectfully disagree. I would much rather deal with ownerhip and related issues than deal with dynamic typing fallout. Unfortunately, I am stuck in Python land for some foreseeable future.
> But there's whole lot of java code and libraries out there.
That is true about every other popular language. Rust might not have the ecosystem, being a lot younger, but why would anyone pick Java out of all the other options?
As far as I can tell, the only two reasons people continue coding in Java are "We have an existing Java ecosystem, it is easier to continue doing what we have been doing than to switch" and "Java is what we know best and it is easier to use Java than to learn new technology stack".
Scripting? I'm not gonna write a 50 LOC single-use script in Rust.
JVM tooling is also pretty nice. Can you trivially get deep app metrics from any Rust program?
1. Rust-the-language doesn't know anything about allocations, so strictly speaking, it cannot. Furthermore, without a GC, it's unclear how it could in the first place, given that a compaction would invalidate all pointers to that memory.
2. Certainly not to the same degree as the JVM, given that there's nothing inherently to inspect, like there is in languages with a runtime. You can instrument your own application, but that does mean doing the work yourself, and you only get as good of results as you put in in the first place.
There are some platforms and ecosystems that pour 90% of the effort into a handful of popular niches, but once you leave that comfort zone you’re on your own. Elixir can feel like that; Ruby/Rails too back in the day.
It just has language features that make sense for the web, which is a huge sector. (parallelism, memory safety, speed, JS interop)
Your overall point is absolutely correct though. We couldn’t have done this without LLVM.
- https://rust-lang.zulipchat.com/#narrow/stream/189540-t-comp...
However, modulo LLVM bugs, the worst to be expected from a frontend selecting a different set or order of LLVM passes would be a loss of codegen quality, which may not be particularly crucial for non-C++-shaped languages.
However not all of them are. Back in the pre-async/await days you'd end up with some really gnarly implicit types in some really big functions with lots of returns. Then sometimes you'd make a mistake on one of the returns and the compiler would assume that your mistaken type was the right one & point out all the places where the actually correct type was returned. Then you'd have to go hunt for where you needed to add a `as Box<Future<Item = _, Error = _>>` to get it to compile again.
I haven't hit any issues like that since I started converting the gnarly stuff to infinitely simplier async/await code, but I have to imagine those kinds of issues are still lurking in other areas where you can't get away from complicated code.
Even with that I whole-heartedly recommend anyone that's thinking about it just go try it out and/or read some of the book. It's a delightful language to work with. I have way more fun working with Rust than I have with any other language. It takes care of so much of the BS that I don't want to have to think about, so I can just think about the interesting stuff.
And will do, if I encounter them.
Years past we spent a lot of time honing general common errors to a pretty neat level, then we moved on to detecting specific cases. I would say that the quality of the output is a bimodal distribution with a cluster of really high quality for common errors, and a big but shrinking cluster of confusing errors for more advanced features.
To give an example, async await relies on three things: syntactic sugar (which means there's generated code that's not in the user written code), associated types (that had badly confusing type errors) and impl Trait (that had confusing type errors). The later two features didn't receive much love for a while because they were advanced features that a newcomer would be unlikely to hit, with exceptions for crates doing interesting but confusing things with them to provide a nicer API. But when the dust started to settle aroune async/await it was clear to me that people would be seeing the stabilization announcement, consider trying rust and jump straight to very arcane errors that would now be very easy to hit. This made me and a few others actually spend the time to drastically (if not entirely) improve those errors ahead of async/await stabilization, with the nice side effect that those features are also up to the level of quality that we want for newcomers. The syntactic sugar part also plays a part on how you explain succinctly and clearly that the type or inference error you're seeing comes from a return type materialized out if the aether, but I think we've gotten good at that.
If you will be picking up Rust to try it out, I encourage people to use nightly, not because of features but rather because we have diagnostics improvements daily and they can be the difference between having to read the book to understand how to fix your issue and just following the compiler's lead. As an example, you can see this recently landed PR: https://github.com/rust-lang/rust/pull/68267
I've recently been using Flutter and it's a delight to get an error message like "you did X, but Y was uninitialized, so you need to put a call to Y.Z in your code first". When my problem is some conceptual mistake, a traditional error message, like "null pointer error in Y" just confuses me, because I don't even know what Y relates to what I'm doing.
I love that you and others are taking the time to see what really went wrong. Partly because it solves my immediate problem, but also because I know that's exerting backpressure on confusing language and library features, which I hope will drive more platform improvements down the road.
It's still possible to hit some edge cases where error messages aren't as helpful as they could be. But these are considered bugs and so will hopefully be fixed.
Some errors are admittedly worse, and people like estebank are actively working on improving them.
I am in that position at the moment. We are a multi-language shop, and there's some components that make sense to write in Rust.
But actually a lot of the big tech companies are using rust, they’re simply not advertising jobs.
At this point, this is not true. There are significant production deployments of Rust. Heck, Rust code helped me post this comment, and read yours! (Okay that's strictly not true because I have Chrome on this computer, but had I posted from my phone, or from my other computer, it would have been Firefox.)
A lot of why this is hard in C/C++ is memory management. Even if you had a package manager it would be hard to compose libraries together unless they were designed together (like boost for C++).
The Standard C++ Library supports user-managed allocators for all the containers, heavily used in embedded, low-latency, and real-time applications to get deterministic performance.
Lack of standard support for user-provided allocators indicates immaturity. E.g., C++ lacked them until 2011.
There are subredits of every kind, even great ones. But they change with time.
Another point is the tight integration with cargo, which may lead to the same dependency problems that npm has. But it will cause significantly more struggles because of licensing issues. While most npm code was running on servers, it didn't matter. In the embedded space companies typically insist to have every library checked by a lawyer before it gets shipped with the product, which will make cargo essentially useless.
The last one is the community. In fact I think a programming language is to the largest part about the people using it. And Mozilla sadly created a hype and a hostile community by aggressively evangelizing the language.
That's not a bad description of Cyclone, Rust's direct predecessor. It went nowhere because it was so "conservative" over C as to be deeply unintuitive, and it lacked features that matter to modern programming. Rust is nonetheless far less complex than C++, and that in itself makes it a plausible C replacement.
Before Rust, everyone simply assumed that it was impossible to create such a programming language since it would have otherwise been what everyone used, and now that it turns out that it's possible, it's clear that it's the best solution and what should be used in the future whenever possible.
https://stevedonovan.github.io/rustifications/2018/08/18/rus...
Control over low-level details is something that exists and should be in any system-programming language. How is that even a benefit? It is a niche feature.
Memory safety? You can circumvent it by using "unsafe" and as recent Actix case shows it can propagate really deep. Should I read the code of any other Rust library now? What if I am a programmer newbie driven by the hype without a clue what does memory safety entail? What-ifs... I won't talk about compile times and the fact that in order to compile some cli tool one has to download ~200mb of rust "stuff". This is simply insane and I refuse to understand how is this considered to be not an issue.
Rust ecosystem is probably its strongest point right now which is somewhat funny because it grew out of beliefs in the above "benefits". So, not hyped, sorry.
Isn't the point the combination of features is unique?
It seems like if I invented a functional sledge hammer that only weighed 1oz, you'd say:
> It can hammer big nails? So can other sledge hammers!
> It only weighs 1oz? So does a plastic toy hammer!
> So I don't care.
This is actually an explicit design decision; it means that the analysis is tractable, and that you don't get spooky-at-a-distance error messages.
The number of times I see people recommend "just cut and paste in whatever type the compiler said it expected ..." just makes my hair curl.
It also tends to make the code more obscure and unreadable, sometimes completely so.
Promoting Cargo as a way to build C++ and C programs could be a way to establish Rust underpinnings. Cargo is better than most other build systems now used for C++, and with C++20 getting modules, the fit is improving.
The Oxidation page [1] seems to give an overview where it is.
If Rust manages to actually supplant C or C++ it would very likely require decades and generational change.
https://insights.stackoverflow.com/survey/2019?__hstc=188987...
There are some backend web development deployments at other companies, but the ecosystem has been changing a lot over the last year. It’s starting to settle down though. Expect more “Flask” and less “Rails.”
I just made the point, because from security point of view, we all have to gain no matter what safe systems programming languages get adopted.
It doesn't need to be Rust vs the others, in what concerns improving the current state of IT security.
For example, Nvidia is now adopting Ada/SPARK for security critical firmware.
#include <string>
#include <iostream>
#include <string_view>
int main()
{
std::string name { "Vivian" };
auto nickname = std::string_view { name }.substr(0, 3);
name.clear();
std::cout << "Hello there, " << nickname << "!\n";
}
which will happily compile, but whose behavior is undefined.`std::string_view` references that copy. `std::string::clear` destroys it, leaving the `std::string_view` dangling.
1. Print the nickname before clearing the name so that nickname is still valid when you print it (swap lines 4 and 5).
2. Make nickname a copy of the first three bytes of name, rather then a reference to the first three bytes of name.
let nickname = String::from(&name[..3]);In C such code would compile, what's worse, it would work and behave correctly most of the time. If your code was more complex (for example using threads) you would get a bug that the code would work fine most of the time, but once in a while it would display garbage.
The `nickname` variable holds onto a reference to the substring of `name`. Subsequently `name` is modified (cleared).
This forces you to think about the behaviour. Should the nickname remain constant even if the backing `name` string changes? Probably yes, so you'd take a copy at that point.
> One of the biggest benefits of using a systems programming language is the ability to have control over low-level details.
I think this is a misguided conflation of "systems programming" and "low-level programming", those are separate things. See http://willcrichton.net/notes/systems-programming/
C++'s and Rust's ranges stretch from C's at the bottom up to near ML's. Java is stuck in a little space in the middle.
Rust is not doing that, and that's a hurdle.
In my view, the "C flavor" is very important because it's a standard of how people are able to read code. This has nothing to do with the C programming language, I'm just saying that simplicity is what made C popular and easy to use. I can understand that Rust brings very nice innovations, but in my view it should have done so while keeping the "C flavor" as most as it could have.
I admit it's a conservative view, but I tend to believe that a language can be more easily adopted if at its most basic level, it's simple to read for beginners. Rust is not great: I'm not talking about its learning curve, I'm just talking about the "flavor". A language is a tool of expression and communication, features matter less than readability.
if (foo) bar();
is if foo {bar();}
Same length, but without possibility of "goto fail" error.Rust may look ugly, especially with generics, but on the technical level there isn't much wrong with the syntax. It's quite readable once you know it. And you can grep function definitions!
fn add2(x: i32, y: i32) -> i32 {
x + y
}
variable declaration is varname: type, it makes it very hard to read when declaring arrays or other thingsreturn type is after the paren
the let keyword seems redundant/unnecessary, a little like var in js
Those are good example of "c flavor". I guess they're easier to parse, but still I would rather have C flavor instead.
I personally find the "varname: type" syntax easier to read.
Also, keep in mind that Rust has type inference, so you can say things like "let foo = 37;", and the compiler will infer the type of "foo". The "varname: type" syntax is more compatible with optionally specified types, because it nicely fits in if you want to add the type info explicitly: "let foo: u64 = 37;".