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 ...
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'...
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 ...
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 ...
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&...
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 ...
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 ...
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 ...
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 ...
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 ...
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;
...
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 ...
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, ...
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 ...
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 (...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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.
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
rust × 49c++ × 6
design × 4
design-patterns × 4
architecture × 4
concurrency × 4
go × 4
object-oriented-design × 3
c × 3
language-design × 3
trait × 3
java × 2
testing × 2
domain-driven-design × 2
multithreading × 2
compiler × 2
exceptions × 2
functions × 2
error-handling × 2
server × 2
type-systems × 2
clean-architecture × 2
golang × 2
c# × 1
object-oriented × 1