Questions tagged [rust]
Rust is a systems programming language focused on three goals: safety, speed, and concurrency. It maintains these goals without needing a garbage collector, making it a useful language for a number of use cases other languages aren't good at: embedding in other languages, programs with specific space and time requirements, and writing low-level code, like device drivers and operating systems.
51 questions
4
votes
3
answers
318
views
The applicability of functional core - imperative shell to a cli program which contains a wrapper around a binary
Not long ago while I was exploring for solutions to test without mocks I've found out about the functional core, imperative shell architecture. It sounds great, I also think that it would play nicely ...
12
votes
4
answers
3k
views
Is the separation of a database process from the main backend process really "good practice"?
In our current architecture, we have a React frontend communicating with a Rust backend via REST calls. We are considering introducing a PostgreSQL database, and my colleague suggests that we should ...
2
votes
3
answers
320
views
Aggregate with a huge list of value objects
I'm currently reading "Implementing Domain-Driven Design" while going through the code samples, but I'm having trouble modeling aggregates that stores a huge list of value objects. For ...
4
votes
4
answers
375
views
Is it reasonable to ignore const functions in Rust and treat them as a premature optimisation?
While I'm far from an expert at Rust, I'm becoming a proficient Rust user; I've written a substantial Rust library that is now being used in production. While I understand what const fns are, I haven'...
0
votes
2
answers
347
views
Intuition behind why Rust's io::stdin().read_line() relies on a side effect?
I'm reading the second chapter of the official Rust book as a pretty seasoned python programmer. As you can imagine, its been pretty jarring, but I'm learning.
My specific question is about whether or ...
0
votes
0
answers
107
views
Is it really difficult to test these “Service” methods in this Rust Clean Architecture proposal? Is there some other catch I'm not considering?
I reproduced a small example of kerkour's Rust Clean Architecture on the Rust Playground.
The code is just an example and the methods code makes no sense at all.
This architecture leaks DB information ...
1
vote
1
answer
242
views
How difficult would it be to extend Rust's compile time checking to dynamic linking?
When compiling Rust, various additional checks are made for correctness. These include bounds checking, borrow checking for multithreading and memory ownership, and the like. Once compiled, these ...
0
votes
2
answers
1k
views
What problem does Rust's "atomic reference count" actually solve?
The Rust programming language offers a Arc atomic reference counting generic type for use in multi-threading environment, since Rc is optimized for performance in single-threaded applications, and ...
0
votes
2
answers
107
views
Deserializing serial protocol enums: Recoverable or unrecoverable errors?
I am currently implementing a library in Rust that implements a proprietary serial protocol.
The protocol specifies several enum values, that mostly are returned by the hardware as u8s (bytes), but ...
0
votes
3
answers
199
views
Is there a distinct optional type semantically representing a value that *needs to be calculated later*?
Semantically, C++ std::optional, Rust Option<T>, and other optional/nullable types represent a value that can be present or absent: you have to handle both cases, or you can opt-in to crash.
Is ...
1
vote
0
answers
166
views
How can I represent a transformed AST between compilation stages?
I'm writing a compiler in Rust. I've reached the point where I have an AST and am ready to do symbol resolution, type-checking, etc. But, I'm unsure of how to represent an AST as containing "...
0
votes
2
answers
125
views
What are the pros and cons of structuring an application as a pipeline of functions mutating a shared struct?
I could not find anything regarding this question except some lecture notes about game design and a book which describes something similar but not quite the same.
General Description
The approach is ...
1
vote
0
answers
148
views
Designing an API adapter with multiple authentication types
I'm building an HTTP API database adapter that has an authentication component. Users can authenticate using password, federated login such as OAUTH, and JWT.
My initial design is something like the ...
27
votes
6
answers
28k
views
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? I mean, a safer language means that more code is written inside the compiler, right? More ...
2
votes
1
answer
673
views
Representing Rust enums in databases
Although I love Rust's enums (and the idea of making illegal states unrepresentable), I'm struggling to represent (ironic, yes) them in databases like PostgreSQL when the variants have data associated ...
-2
votes
1
answer
90
views
How would data be dynamically queried in rust
Say there is an user that wants to query a random element during runtime from a dynamic struct. The type of the data is unknown, but all the data is either a integer (size unknown), float, string, or ...
2
votes
1
answer
2k
views
Build a Rust project using Clean architecture and DB transactions in the same DDD bounded context
This is just an example of an (still incomplete) real-world project written in Rust using a clean architecture: https://github.com/frederikhors/rust-clean-architecture-with-db-transactions.
Goals
My ...
0
votes
1
answer
91
views
Handling IO operations through a server and building the UI using a separate framework/application
I am trying to build a file editor, and I wanted to build the UI using Flutter. However I wanted to implement IO operations (reading a file, applying changes, etc) in Rust.
The reason I would like to ...
1
vote
1
answer
362
views
Creating a new type as slice of strings in Rust?
I have a little bit of experience with Go, that I have been trying to use as a reference point to wrap my mind around Rust via a cards game I wrote in Go that I would like to now write in Rust.
I know ...
5
votes
1
answer
576
views
Why does Rust allow a leading `|` in or patterns?
In the code snippet found in this tweet, pattern matching is used like this:
let (|x| x) = |_| Some(1); // same as `let (x | x) = |_| Some(1);`
Which threw me off. Rust's pattern syntax is defined as:...
-2
votes
1
answer
166
views
What is a right way to handle requests?
A social network has API, but also it has some limitations like the amount of requests that can be done in one second (let's say API will give an error, if it accepts more than 3 requests per second)
...
1
vote
2
answers
366
views
Is it a good practice to allocate memory size to data types?
I'm learning rust and curious to know the purpose that we can allocate memory sizes to data types. I'm not clear yet how this could be an advantage. It can definitely save up some memory space but I ...
4
votes
5
answers
2k
views
(How) can the circle-ellipse problem be solved by using composition rather than inheritance?
I was reading about composition over inheritance and came across a question about solving the Circle-Ellipse Problem in Object-Oriented Programming. This kind of problem is often used as an example of ...
2
votes
1
answer
3k
views
What is the idiomatic way to split code between separate files in Rust?
Coming from the world of C#, where, despite sharing namespaces, it's quite common for every class to have its own file, I find that Rust codebases seem to have a complete different sort of philosophy (...
4
votes
1
answer
568
views
Is there a way to make Rust code more succinct?
Let's take at random a well-written piece of Rust code:
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: Some("Shader"),
flags: wgpu::ShaderFlags::...
2
votes
1
answer
156
views
Rust design when object needs to query outside data for some uses
I am modeling a ship that has contracts. Each contract can either be a fixed rate contract which pays a set fixed amount every day or a market index contract which pays an amount that varies based on ...
67
votes
9
answers
9k
views
Why do "checked exceptions", i.e., "value-or-error return values", work well in Rust and Go but not in Java?
Java has "checked exceptions", which force the caller of the method to either handle an exception or to rethrow it, e.g.
// requires ParseException to be handled or rethrown
int i = ...
2
votes
1
answer
495
views
Rust/specs – what is the good/idiomatic way of design level walls/static objects handling in an ECS architecture
Warning: long question ahead, don't be afraid, I just tried to be as precise as possible about details for who wants them but many paragraphs are skipables if you already understood what I want ;).
...
3
votes
1
answer
3k
views
Is this enum/trait a good way to implement polymorphic design in Rust?
this is my first post on here, and I'm wondering about a good rust implementation with traits on enum types. I want to know if using an enum w/ a trait as a generator of different code is viable like ...
4
votes
1
answer
4k
views
Managing const strings in a Rust project
I'm looking for a way to manage constant strings in a Rust project. The hope is to manage my strings from a single file and avoid having the same string literals all over the place in the project.
...
10
votes
1
answer
452
views
How to design a good generic tiled image downloader?
Tiled images
Tiled images are large images that have been split in smaller square tiles.
There are several tiled image formats, with different ways of organizing
the tile files.
A tiled image on ...
3
votes
2
answers
3k
views
Can Rust replace the C or C++ programs in the Future? [closed]
I recently read an interesting Article that, due to a memory Overflow error Power system of Boeing will collapse and turns the Flight into a Flying Brick.
This is a low level memory Handling bug. ...
5
votes
3
answers
542
views
Why datatypes are marked as thread-safe instead of procedures?
In Rust, Send (or Sync) marker traits are used to indicate whether a value of a type (or a reference to that) can be worked on within threaded context.
However, it is an attribute of a function or a ...
1
vote
1
answer
246
views
Signature for a Rust method that modifies object but might also drop it?
I'w writing a program in Rust that basically pushes data through a pipeline of steps that transforms it in different ways. The data is represented by an Entry, and I am designing a Step trait for the ...
10
votes
5
answers
850
views
Is it possible to programmatically evaluate safety for arbitrary code?
I've been thinking a lot lately about safe code. Thread-safe. Memory-safe. Not-going-to-explode-in-your-face-with-a-segfault safe. But for the sake of clarity in the question, let's use Rust's safety ...
7
votes
0
answers
427
views
Rust and lifetime elision rules for structs?
In the Rust documentation, under the structs section, they give this example of how structs need lifetimes when they contain references:
struct Foo<'a> {
x: &'a i32,
}
because
We need ...
4
votes
1
answer
658
views
In what ways is Rust a "concurrent" language?
Rust is advertised as a "concurrent" language, what does this mean specifically and how is it different from other languages such as C++?
2
votes
1
answer
131
views
Section of program needs root access
I have to perform a number of 'housekeeping' tasks that need root access (Linux, Debian). Generally one time only, but I do need to check that they have been performed.
I don't really want to run the ...
-2
votes
1
answer
360
views
Rust and composition [closed]
I have a piece of code.
struct HasName {
name: &'static str
}
trait CanGreet {
fn greet(&self);
}
impl CanGreet for HasName {
fn greet(&self) {
println!("Hello {}", ...
4
votes
1
answer
721
views
Can an object be moved through a match expression? [closed]
I am using Rust 1.15.1 and, recently, I stumbled over a problem I could not find a straight forward solution for.
When you look at examples using pattern matching they usually destructure the object ...
8
votes
2
answers
754
views
Comparision of modeling with inheritance vs idiomatic trait based composition
I recently I started learning Rust and Scala and what struck me was the lack of inheritance model that I'm used to in C++ and Java.
Although I can model simple things with structs and traits in Rust,...
6
votes
1
answer
2k
views
Why does Rust require external linkers? Any other similar languages?
Rust needs external linkers (e.g. GCC) to generate final output. Why doesn't it provide a bundled one? Are there any languages that does the similar?
6
votes
1
answer
465
views
Implementing a construct like Rusts `match` in C?
I'm writing a compiler that compiles to C, one thing I'm attempting to do is implement a construct like Rust's match:
// { some function
let mut foo = 32;
match foo {
3 => return "...
19
votes
1
answer
2k
views
Is it possible to achieve Rust's ownership model with a generic C++ wrapper?
Looking through this article on Rust's concurrency safety:
http://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html
I was wondering how many of these ideas can be achieved in C++11 (or newer)....
40
votes
2
answers
4k
views
How does Rust diverge from the concurrency facilities of C++?
Questions
I am trying to understand whether Rust fundamentally and sufficiently improves upon the concurrency facilities of C++ so that to decide if I should spend the time to learn Rust.
...
28
votes
8
answers
6k
views
Using a "strong" type system in the real world, say, for large-scale web-apps?
I know this is a very broad, ambiguous, and possibly philosophical question. To an extent, that the most important keyword in the question - "strong" type system - itself, is ill-defined. So, let me ...
7
votes
1
answer
566
views
What is the most generic way to provide a variable amount of outputs from a Rust function?
I am currently writing an API for machine learning algorithms in Rust and I would like for a single genetic algorithm, artificial neural network, or Bayesian network to provide multiple outputs so ...
4
votes
1
answer
4k
views
Publishing a crate containing both lib.rs and main.rs files
In the Importing External Crates section of the Rust book the author creates main.rs file in an already existing library project. I randomly picked up a bunch of crates from crates.io, examined their ...
11
votes
2
answers
4k
views
Rust-style error handling in C++
I've been reading some articles on how Rust does error handling using the Result<T, E> type and to me it seems like a hybrid best-of-both-worlds (exceptions and return codes) solution which can ...
0
votes
2
answers
129
views
problems compiling a function with a trait Add in Rust [closed]
I'm trying to write a generic function summ in rust - but to no avail. Could someone please elucidate the problem?
fn summ<T:Add>(a:T,b:T)->T {
a+b
}