Definitely not Python. Rust is very nice though.
Definitely not Python. Rust is very nice though.
It’s funny how it used to be expensive. In Danish we have a saying to say someone is poor if “they can’t even afford salt for their egg”, as if an egg is cheaper than salt. Because it actually used to be that way.
You can’t get data races at least, and in practice it’s very difficult to get actual race conditions.
(I assume you left out a “not” there)
You should definitely give Rust a shot. It is only conceptually similar to C++ but otherwise very different.
interfaces with external IO
How would you run into a race condition like this with safe Rust? You can’t share mutable file handles between threads for example. I’m not sure you’re correct in saying its still possible. Even if it is, it doesn’t sound easy.
Honestly I would consider that a bit weird. At the very least, old-fashioned. If you like Java, it makes me think you haven’t tried a better more modern language to compare it with.
I don’t particularly like Java, but I use it because it pays the bills. Similarly, I use C++ (which I prefer) when my work requires it.
I mean, anon is not arguing against that. They’re saying the language is shit regardless of how much it is used in business. I don’t think they are entirely wrong.
Threads giving you race conditions? All concurrent programming will do that if you’re shit at it.
Well, if you write Rust then there won’t be race conditions.
Java is industrial strength for professionals.
Disagree. It’s an outdated tool today. Professionals would not choose it for new projects.
You dropped this: /s
Please don’t accuse me of lying. Upvotes obviously influence the feed. I am aware that hexbear has disabled downvotes.
If you were on reddit at the time when /r/the_donald was a menace, you should know how harmful even upvotes can be.
One more vote for using WASM. Using WASM has the benefit of allowing the user to use almost whatever language they want, as long as it can compile to WASM. So the user doesn’t have to learn some bespoke scripting-specific language.
Their votes will still influence your feed though. In that way, you don’t get to choose entirely freely what you want to see, they will influence what you see as well.
Definitely let go. Rust has some OOP features, but it’s mostly just the OOP idea of interfaces, which Rust models with traits. You can also do dynamic dispatch, which is another OOP feature, but you should almost never use this in Rust unless you absolutely have to. Then there’s encapsulation which is hugely important in Rust too, but yea outside of that kind of thing, I don’t think OOP patterns are too useful. Honestly, if you ask me, many of these “OOP patterns” are really just solving problems that OOP causes in the first place.
Feel free to ask any other questions.
Thanks for explaining further, it’s a lot clearer now what you want to do. And no, I don’t think this DAO thing is idiomatic for Rust and you probably don’t want to do it like that. I’m not familiar with the pattern though, I’m not too much into OOP myself.
Anyways, I’ve worked a lot with axum and sqlx before so I can tell you what I’d do.
I am writing an axum backend which (like other backends) needs to do stuff in the database. As some endpoints update the database (and sometimes over multiple sql statements) I want to pass around the transaction as this embodies the connection I am using to update the database.
This makes sense. You just want a database connection pool (sqlx provides this) in your axum state so your handlers can get connections to the database.
To separate the axum stuff (parameters, urls and whatnot) from the actual database logic, I’ve first pulled out all the database interactions into separate functions. Because those functions are logically groups (e.g. stuff happening with invoices, others with contacts etc), I thought it was a good idea to create a “dao” struct (and agreed: my OO brain kicked in here which might be debatable). This would group the interactions for each logical domain into a short-lived data access struct.
Again, not sure what this DAO struct actually entails, but what I would do and have done in the past is just do exactly what you said before: “I want to pass around the transaction”. So I would make my functions take the Transaction
struct from sqlx (IIRC it has some type parameters and a life time but you can use a type alias to make it less verbose) and then I would just use that transaction in the function to call SQL. If you have a function that needs access to the database but doesn’t need a transaction, you can just use a plain connection instead of a transaction.
To prevent passing around the transaction/connection, i wanted to pass that along during construction
I’m not sure what you mean with “pass along during construction” but in any case, why do you want to avoid passing the transaction/connection? I feel like that is exactly what you should do. That is what you need to do anyway. Rust favours explicitness and you need to pass the transaction/connection to functions for them to use it, so just pass it.
That’s not what I said. Read about the XY problem and then come back and explain what you actually want to achieve, and give some more information like code examples.
This screams of XY problem. You’ve gotten a new problem from using this method and you’re asking for help for that, but probably there is an underlying better solution that solves your actual use case without running into this problem at all.
Tbf I do think it qualifies as title gore and at the very least it is missing a comma before “ending”
“Wikipedia concludes that Israel is committing genocide, thus ending an editorial debate”
Such a gem of a movie