New answers tagged rust
1 vote
How can I take an integer string modulo 4 without overflowing u128?
@phuclv observes that to get the remainder when dividing by 4 you just need to take the remainder of the last 2 digits. It works regardless of the size, no need to parse the number at all. In fact ...
0 votes
How to use c2rust on Windows?
Yeah this is one of those tools that looks like it should work on Windows… but doesn’t really 😅 I tried this before and ran into the same issue. The problem isn’t your setup, it’s that c2rust isn’t ...
Advice
2 votes
0 replies
0 views
How do I stop rewriting my incomplete projects in different languages?
figgyfarts is not alone in this. I've been doing the same thing for over 30 years and I don't consider it a problem.
2 votes
Accepted
Decoding and validating .net generated JWT in Rust
The problem is that your token is not using a standard JWT alg. jsonwebtoken expects values like RS256, but your token has: "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" That’s an XML ...
Advice
1 vote
0 replies
0 views
How do I stop rewriting my incomplete projects in different languages?
This depends what's causing you to switch languages. If you're hitting major problems with a given language, preventing you from implementing what you're trying to do, then that's a sign it's worth ...
Advice
0 votes
0 replies
0 views
How do I stop rewriting my incomplete projects in different languages?
What made you decide to rewrite them, instead of finishing it off in the language you were already using? Knowing your motivation for doing that would help us understand the situation and how you ...
Advice
1 vote
0 replies
0 views
How do I stop rewriting my incomplete projects in different languages?
Thank you for the advice! I think the Java GUI path is least likely to work out. The PHP version could be nice for viewing the page from my phone (if I managed to host it on my Raspberry Pi), and the ...
Advice
1 vote
0 replies
0 views
How do I stop rewriting my incomplete projects in different languages?
Thank you, that is some great advice!
Advice
10 votes
0 replies
0 views
How do I stop rewriting my incomplete projects in different languages?
You don't. You code for fun because you want to learn things, not beause you want to complete projects. Once you've learned something, it's ok to move to something more interesting. Leave the boring ...
Advice
5 votes
0 replies
0 views
How do I stop rewriting my incomplete projects in different languages?
I've always tried to match the language to where the data exists and how it will be used. If I'm pulling information from a remote server and I want to make it available as a web-interface, then PHP, ...
0 votes
Unable to build/compile `yeslogic-fontconfig-sys vX.X.X` with Rust on Ubuntu system
apt install librust-yeslogic-fontconfig-sys-dev
2 votes
Accepted
Rust Validator on Double Option Fields
Funny workaround - some derive macros that have special handling for certain types often determine that syntactically (i.e. by tokens only) - so you can use a type alias to thwart it: use chrono::{...
1 vote
Is this a sound method to check for AsyncFnMut implementations that return Send futures?
As far as I can tell this is not sound, because AsyncFnMut implementations can be generic over lifetimes and the call to check_thread_safe may infer a different lifetime than a call to the closure. ...
0 votes
Cannot start Tauri dev server - "beforeDevCommand terminated with a non-zero status code."
My problem was that dx was not on the path. I solved this by running cargo install dioxus-cli and then adding export PATH="$HOME/.cargo/bin:$PATH" to my path.
0 votes
Is there a rustc equivalent of -Wall -Werror?
In addition to what's already been said, you can also add this to Cargo.toml : [lints.rust] warnings = "deny"
Best practices
0 votes
0 replies
0 views
How to build Anki on Linux (Lubuntu 24.04)?
So far, I don't understand it. ./ninja format && ./ninja check is new for me.
Advice
0 votes
0 replies
0 views
Downgrading Send to !Send in Async runtime with work stealing
Work-stealing should still be possible, it would be under a tighter bound, which would be when every future within is .await'ed (Poll::Pending right). If I am not wrong this is "possible" ...
Best practices
0 votes
0 replies
0 views
How to build Anki on Linux (Lubuntu 24.04)?
See the comments above about using the script check mentioned in the MD file. Have you tried that? If you have issues with that open a question.
Best practices
1 vote
0 replies
0 views
How to build Anki on Linux (Lubuntu 24.04)?
There's a md (it's for AI agent but) saying how to build, using script check and ninja
4 votes
Accepted
Propagate errors across task boundaries in an ergonomic way
Typically you see Box<dyn Error> for non-Send errors, otherwise you see Box<dyn Error + Send + Sync> for boxed errors that can cross thread boundaries. If you add the missing Sync bound, ...
Advice
0 votes
0 replies
0 views
Downgrading Send to !Send in Async runtime with work stealing
That's what the section of the docs I linked talks about, but it requires a second thread-local runtime. I'm not aware of any way to do this on a single multi-threaded runtime.
3 votes
Accepted
Sending and receiving data between two threads based on a time condition
You don't need the data variable since you reassign it completely inside the loop. Moreover, since you don't mutate them, you can replace your Vec<String> with Arc<[String]> which is ...
2 votes
Accepted
Bevy systems ordering with .after() vs .chain()
The referenced documentation is not easy to find. It has since moved to here. It answers this question: fn before<M>(self, set: impl IntoSystemSet<M>) -> ScheduleConfigs<T> Runs ...
Advice
0 votes
0 replies
0 views
Downgrading Send to !Send in Async runtime with work stealing
The way I understood it, the OP is asking how to run a !Send task in a runtime that is otherwise work stealing. IOW, that specific task would run on the thread where it is created, but the runtime may ...
Best practices
0 votes
0 replies
0 views
How to build Anki on Linux (Lubuntu 24.04)?
I was searching there before you have written and searching in README, there is not manual how to build it. I'm asking rather here, because I don't have experience with Rust yet (I will learn it) and ...
Advice
0 votes
0 replies
0 views
Downgrading Send to !Send in Async runtime with work stealing
work-stealing between !Send tasks How might this work, given that the way work-stealing happens is by sending tasks to another thread that is free? It sounds like you're asking something akin to &...
Best practices
0 votes
0 replies
0 views
How to build Anki on Linux (Lubuntu 24.04)?
If you have trouble with the build, you have to ask the authors of the repository. Here is the issues page of this repository.
Best practices
0 votes
0 replies
0 views
Naming methods for a generic type in Rust for backwards compatibility
I added the best solution for this.
Best practices
0 votes
0 replies
0 views
Naming methods for a generic type in Rust for backwards compatibility
Okay, here is the solution after collaboration with Morgane from Rust Community's Discord. You do this in steps, possibly over months or years, for users to adapt to the new methods. Add fn new_u32() ...
Best practices
0 votes
0 replies
0 views
Naming methods for a generic type in Rust for backwards compatibility
This would work, but I do not see how it's any better than adding `_general` methods.
Best practices
0 votes
0 replies
0 views
How should I pass user secrets from my frontend to my backend with Tauri?
From my point of view, something like a xss attack on your javascript side is more dangerous. Tauri use IPC calls that are transported via os level pipelines rather than Internet protocols. Maybe ...
0 votes
Can't override windows key with tauri window focused
here's my approch: On ur rust side, typically in ‘lib.rs’. add .device_event_filter(tauri::DeviceEventFilter::Always); after tauri::Builder e.g.: tauri::Builder::default() .device_event_filter(tauri:...
Best practices
0 votes
0 replies
0 views
Provenance of pointer fields of structs obtained from `std::ptr::read` over a buffer coming from the C FFI
Thanks, what do you mean by “a valid Rust allocation that has been exposed”? If I pass a Rust pointer to the C side in the same way (using std::ptr::write on a byte array), and then that pointer ...
Best practices
0 votes
0 replies
0 views
Provenance of pointer fields of structs obtained from `std::ptr::read` over a buffer coming from the C FFI
The compiler has to assume exposed provenance unless you used the strict provenance APIs (which you likely should not for a C allocated object). So your code will work assuming that the pointer points ...
Best practices
0 votes
0 replies
0 views
Naming methods for a generic type in Rust for backwards compatibility
Does this break compatibility? struct BitVecGeneric<B> { store: Vec<B>, } impl<B> BitVecGeneric<B> { fn new() -> Self { Self { store: Vec::new() } } } ...
Best practices
0 votes
0 replies
0 views
what pattern allows more freedom in rust when dealing with structs that have intertwined calls?
This is just not "✨the rust way✨" of designing struct. Try to uncouple the two data type. You can always avoid mutual reference and just pass reference when needed. struct Struct1 {} struct ...
Best practices
0 votes
0 replies
0 views
what pattern allows more freedom in rust when dealing with structs that have intertwined calls?
In Rust you cannot borrow a value while it's exclusively borrowed, full stop. There is no way around this -- it is a fundamental rule of the language. All RefCell does is move this check to compile ...
Best practices
0 votes
0 replies
0 views
what pattern allows more freedom in rust when dealing with structs that have intertwined calls?
Should have used a troubleshooting/debugging question.... learning about these different options... let's see how far it goes using this option.
Advice
0 votes
0 replies
0 views
Rust: How to println!() the numerical value of an enum variant?
Okay, this one works nicely and also warns me about overflows. Thanks!
Advice
0 votes
0 replies
0 views
Rust: How to println!() the numerical value of an enum variant?
Said that, the idiomatic way is to implement impl From<Pets> for usize ... which can be as simple as returning self as usize from from() or having some more sophisticated logic. Or you could ...
Advice
2 votes
0 replies
0 views
Rust: How to println!() the numerical value of an enum variant?
println!("{}", my_pet as usize); Better yet, add #[repr(<your numeric type>)] on the enum to make sure it is actually representable as the number you want to print.
Best practices
0 votes
0 replies
0 views
How to accept a closure that accepts a closure?
This is not possible with a closure without using dynamic dispatch, but is possible with a helper trait. trait WithFunction { fn with_function(self, f: impl FnOnce()); } The only downside is that ...
Best practices
1 vote
0 replies
0 views
Idiomatic rust for sorting a pair of integers (a,b) such that a < b
Maybe so? For readability: fn swap_unless<T>(c: bool, x: &mut T, y: &mut T) { if !c { std::mem::swap(x, y) }} swap_unless(a < b, &mut a, &mut b);
Best practices
0 votes
0 replies
0 views
Idiomatic rust for sorting a pair of integers (a,b) such that a < b
Will probably take time.. looks like it depends on the unstable const traits feature, which is much more interesting by itself.
Best practices
0 votes
0 replies
0 views
Idiomatic rust for sorting a pair of integers (a,b) such that a < b
Nice find. Once stable, this will be the thing to use.
0 votes
why can't I use Rc::clone() directly when providing a parameter to a method?
I think @kmdreko's is a good answer (and even provided details about what is going on) so I am not going to unselect it as the answer.. however, through trial an error I found a different (simpler) ...
Best practices
1 vote
0 replies
0 views
Idiomatic rust for sorting a pair of integers (a,b) such that a < b
I'm not sure any language would have a single idiomatic philosophy at this level of specificity. That said, I personally find the first approach most readable. rustfmt will turn it into a single line, ...
Best practices
2 votes
0 replies
0 views
Idiomatic rust for sorting a pair of integers (a,b) such that a < b
FWIW, there is nightly minimax that sorts pairs of values. Notably, it does the following: pub const fn minmax<T>(v1: T, v2: T) -> [T; 2] where T: [const] Ord, { if v2 < v1 { [v2, ...
Best practices
1 vote
0 replies
0 views
Idiomatic rust for sorting a pair of integers (a,b) such that a < b
I would instinctively follow this anyway since I would typically want to rename from thing_a and thing_b to thing_min and thing_max.
Best practices
0 votes
0 replies
0 views
Idiomatic rust for sorting a pair of integers (a,b) such that a < b
While I'm not as familiar with Rust, if there's an rust-esque way of writing (a, b) = (min(a, b), max(a, b)) it'd be the most clear imo. It looks like you effectively have that via a.min(b) and so on.
Top 50 recent answers are included
Related Tags
rust × 43948rust-cargo × 2381
borrow-checker × 1897
lifetime × 1873
traits × 1837
rust-tokio × 1278
generics × 1175
serde × 975
iterator × 931
multithreading × 781
string × 627
closures × 619
reference × 607
vector × 596
struct × 581
ffi × 573
macros × 572
ownership × 561
actix-web × 529
enums × 525
asynchronous × 483
arrays × 451
types × 446
rust-diesel × 438
webassembly × 424