Skip to main content
69 votes

How can Rust be "safer" and "faster" than C++ at the same time?

You seem to have quite a bit of misconceptions, which I'll address alongside your question. If anything is still unclear, please feel free to comment on this answer so I can address it Safer Yes, Rust ...
Matthieu M.'s user avatar
  • 15.1k
51 votes

How can Rust be "safer" and "faster" than C++ at the same time?

The assumptions are wrong twice: safer code does not necessarily mean more instructions, when the safety is obtained with safer language design and rules; one language is not faster than another: it'...
Christophe's user avatar
  • 80.6k
45 votes

Why do "checked exceptions", i.e., "value-or-error return values", work well in Rust and Go but not in Java?

From a scientific point of view, checked exceptions can be seen as alternative return values, e.g. Exactly. They can be seen that way, and they should be but they aren't. Using an Error type like is ...
Jörg W Mittag's user avatar
27 votes

Why do "checked exceptions", i.e., "value-or-error return values", work well in Rust and Go but not in Java?

Disclaimer: This will, to a degree, be my own personal take on the problem. I love programming language design, I've thought, read, and discussed a lot of about PL design in general, and the hard ...
Matthieu M.'s user avatar
  • 15.1k
25 votes

How can Rust be "safer" and "faster" than C++ at the same time?

To look at this a different way, Fortran is: Safer than C, because it doesn't have C-style unrestricted pointers. "Unrestricted" here meaning "you can do arbitrary operations on them&...
Philip Kendall's user avatar
14 votes

Can Rust replace the C or C++ programs in the Future?

Actually, the bug in the Generator Control Units that you refer to is not the kind of memory handling bug that Rust (or any language with fixed-size integers) can protect against. An internal counter ...
Bart van Ingen Schenau's user avatar
12 votes
Accepted

Is it possible to achieve Rust's ownership model with a generic C++ wrapper?

C++ has three ways to pass parameters to a function: by value, by lvalue reference, and by rvalue reference. Of these, passing by value creates ownership in the sense that the called function receives ...
amon's user avatar
  • 135k
12 votes

How can Rust be "safer" and "faster" than C++ at the same time?

I have been told that Rust is both safer and faster than C++. If that is true, how can that be even possible? One way Rust does this is that it refuses to compile many programs for which C++ would ...
jpa's user avatar
  • 1,378
11 votes

In what ways is Rust a "concurrent" language?

It might mean two things : Capability of concurrent programming Rust provides standard libraries for creation and running of multiple concurrent executions. Mostly through threading. But this is ...
Euphoric's user avatar
  • 37.8k
11 votes
Accepted

What problem does Rust's "atomic reference count" actually solve?

The problem is there is an assumption violation in the sequence of events: object has ref-cnt 1 This implies that object is only visible from 1 Arc reference. In rust this also implies that there are ...
user1937198's user avatar
10 votes

(How) can the circle-ellipse problem be solved by using composition rather than inheritance?

"Composition over inheritance" for the Circle-Ellipse problem means to implement a class Circle by making internally use of an Ellipse object, like this: class Circle { Ellipse e; ...
Doc Brown's user avatar
  • 214k
9 votes
Accepted

Why datatypes are marked as thread-safe instead of procedures?

There is nothing intrinsic to a function that implies thread safety other than its awareness of what shared data may be modified, and how it protects that data. It is the data itself that exists in ...
cyberbisson's user avatar
9 votes

(How) can the circle-ellipse problem be solved by using composition rather than inheritance?

The circle vs ellipse problem (as well as the square vs rectangle) are not an inheritance problem but a problem regarding the contracts and promises made about the initial class. In the first place, ...
Christophe's user avatar
  • 80.6k
8 votes

Is it possible to programmatically evaluate safety for arbitrary code?

What we're ultimately talking about here is compile time vs runtime. Compile time errors, if you think about it, ultimately amount to the compiler being able to determine what problems you have in ...
Neil's user avatar
  • 22.8k
8 votes
Accepted

What is the idiomatic way to split code between separate files in Rust?

In multi-paradigm languages such as Python, C++, and Rust, it is not always easy to see what should be in the same file. In particular with Rust, it is not even necessary to keep data definitions (...
amon's user avatar
  • 135k
8 votes
Accepted

Is there a way to make Rust code more succinct?

No one has accused Rust of being concise. While it does value “ergonomics”, it values control, precision, and stability more. For example, Rust does not support named parameters because the Rust ...
amon's user avatar
  • 135k
8 votes

Why do "checked exceptions", i.e., "value-or-error return values", work well in Rust and Go but not in Java?

There are three parts to error code: Code that generates the errors. Code that handles the errors. Code that just propagates the errors from part 1 to part 2. Most programmers don't give part 3 much ...
Karl Bielefeldt's user avatar
8 votes
Accepted

Why does Rust allow a leading `|` in or patterns?

Rust's optional leading | (vertical bar) in patterns was introduced in Rust RFC 1925. As the motivation section of the RFC explains: This is taking a feature which is nice about F# and allowing it by ...
amon's user avatar
  • 135k
7 votes

Why do "checked exceptions", i.e., "value-or-error return values", work well in Rust and Go but not in Java?

There are some good points made in other answers, but I feel each picks out a different aspect, so I'm going to try to bring them together. Checked vs unchecked errors The idea of statically checking ...
IMSoP's user avatar
  • 5,887
6 votes
Accepted

Signature for a Rust method that modifies object but might also drop it?

Although the Drop trait's drop() method is declared with &mut self, it is not actually possible to call that method directly. Instead you would have to use std::mem::drop() which takes ownership ...
amon's user avatar
  • 135k
5 votes

Why do "checked exceptions", i.e., "value-or-error return values", work well in Rust and Go but not in Java?

checked exceptions can be seen as syntactic sugar for alternative return values This is the modern perspective on this language feature, but the feature was initially intended and used for a ...
meriton's user avatar
  • 4,110
5 votes

Creating a new type as slice of strings in Rust?

Rust is broadly similar to how you would approach Golang, with the caveat that there are some quite relevant differences in the type system. For example: With Golang, you can create new methods for ...
amon's user avatar
  • 135k
5 votes

What are the pros and cons of structuring an application as a pipeline of functions mutating a shared struct?

This sort of thing does show up from time to time in data pipelines, compilers, and certain scripts. Can this way of structuring an application survive in the real world and be used for almost any ...
Telastyn's user avatar
  • 110k
4 votes

Why datatypes are marked as thread-safe instead of procedures?

struct Foo { ... } unsafe sync fn thread_safe_fn(foo: &Foo) { ... } This way, any type can be used anywhere, but only sync functions can operate on shared data; which makes it possible to ...
Erik Eidt's user avatar
  • 34.4k
4 votes

Managing const strings in a Rust project

I don't know if there is a specific "Rust" way of doing this, but I would definitely use const variables as well. Coming from C++, I already do the same thing there (using constexpr). This compiles ...
TheNappap's user avatar
  • 327
3 votes

Is this enum/trait a good way to implement polymorphic design in Rust?

I think there are two ways I would consider implementing this: Using an enum This is usually the appropriate design, in particular when you know all possible types up front. A benefit is the strong ...
Solomon Ucko's user avatar
3 votes

Is it possible to programmatically evaluate safety for arbitrary code?

How safe is safe ? Yes it is almost possible to write such a verifyer: your program just has to return the constant UNSAFE. You'll be right 99% of the time Because even if you run a safe Rust ...
Christophe's user avatar
  • 80.6k
3 votes

Is it possible to programmatically evaluate safety for arbitrary code?

Type system are automatically verifiable proofs of some aspects of correctness. For example, Rust's type system can prove that a reference does not outlive the referenced object, or that a referenced ...
amon's user avatar
  • 135k
3 votes

Is it a good practice to allocate memory size to data types?

The answer is as always "it depends". If it was a feature with no value, nobody would have put it in the language. If it was a feature which always had value, it would be the default. ...
Philip Kendall's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible