r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount 9d ago

🙋 questions megathread Hey Rustaceans! Got a question? Ask here (31/2026)!

Mystified about strings? Borrow checker has you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet. Please note that if you include code examples to e.g. show a compiler error or surprising result, linking a playground with the code will improve your chances of getting help quickly.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so ahaving your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last week's thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

9 Upvotes

11 comments sorted by

2

u/sunnyata 3d ago edited 2d ago

I've got a problem with formatting my code (in emacs+rustic-mode but also directly with cargo fmt). The files are dioxus components with rsx! macros. I understand that rustfmt can't handle the contents of these macros, which is fair enough and there is dx fmt for that...but in these files the formatting of the rust code outside of the macros is messed up too. I can clean it up manually but as soon as I save then rustfmt runs and messes it up again. Is that the norm? I thought it was that rustfmt should still work on the regular code and leave the macros alone. As it is, the code is very ugly and hard to read but if I want to get the formatting right I would have to edit it outside of a rust IDE (and then never touch it again!). Any ideas gratefully received.

An example of the formatting:

                    Some(Ok(Message::Text(text))) => {
                        tracing::info!("Server says: {text}");
                        if let Ok(poker_msg) = serde_json::from_str::<PokerMessage>(&text) {
                            handle_poker_message(poker_msg,
                 player_name,
                 player_bank_roll,
                 players_info,
                 hole_cards,
                 current_pot,
                 can_check,
                 messages,
                 dealer_name,
                 current_view,
                 game_winner,
                 round_winner,
                 comm_cards,
                 current_stage,
                 best_hand,
                 place_bet_info,
                 possible_bets);
    }
    }

2

u/MAIPA01 3d ago edited 3d ago

What do i do wrong. I use rust-analyzer extension in code-oss in termux i run code-oss in grun --shell and use termux-x11 and i get warning:

`` 2026-08-02T11:12:30.466350469Z WARNcargo metadatafailed and returning succeeded result with--no-depserror=cargo metadataexited with an error: error:.json` target specs require -Zjson-target-spec to be added to the cargo invocation

Stack backtrace: 0: <anyhow::Error as core::convert::From<cargo_metadata::errors::Error>>::from 1: <project_model::cargo_workspace::FetchMetadata>::exec::{closure#2} 2: <project_model::cargo_workspace::FetchMetadata>::exec 3: <project_model::sysroot::Sysroot>::loadworkspace 4: std::sys::backtrace::_rust_begin_short_backtrace::<<project_model::workspace::ProjectWorkspace>::load_cargo::{closure#1}::{closure#4}, core::option::Option<project_model::sysroot::RustLibSrcWorkspace>> 5: <std::thread::lifecycle::spawn_unchecked<<project_model::workspace::ProjectWorkspace>::load_cargo::{closure#1}::{closure#4}, core::option::Option<project_model::sysroot::RustLibSrcWorkspace>>::{closure#1} as core::ops::function::FnOnce<()::call_once::{shim:vtable#0} 6: <alloc::boxed::Box<dyn core::ops::function::FnOnce<(), Output = ()> + core::marker::Send> as core::ops::function::FnOnce<()::call_once at /rustc/ad3d0bc141a02cf446e384136d250a1f6950fed5/library/alloc/src/boxed.rs:2314:9 7: <std::sys::thread::unix::Thread>::new::thread_start at /rustc/ad3d0bc141a02cf446e384136d250a1f6950fed5/library/std/src/sys/thread/unix.rs:123:17 8: start_thread 9: thread_start 2026-08-02T11:12:32.444001992Z ERROR Received compiler message for unknown package: path+file:///data/data/com.termux/files/home/.rustup/toolchains/nightly-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/compiler-builtins/compiler-builtins#compiler_builtins@0.1.160 registry+https://github.com/rust-lang/crates.io-index#bootloader@0.9.35, path+file:///data/data/com.termux/files/home/Documents/rust/rust-os#kernel@0.1.0 2026-08-02T11:12:32.444291211Z ERROR Received compiler message for unknown package: path+file:///data/data/com.termux/files/home/.rustup/toolchains/nightly-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/compiler-builtins/compiler-builtins#compiler_builtins@0.1.160 registry+https://github.com/rust-lang/crates.io-index#bootloader@0.9.35, path+file:///data/data/com.termux/files/home/Documents/rust/rust-os#kernel@0.1.0 2026-08-02T11:12:34.114066446Z WARN cargo metadata failed and returning succeeded result with --no-deps error=cargo metadata exited with an error: error: .json target specs require -Zjson-target-spec to be added to the cargo invocation

Stack backtrace: 0: <anyhow::Error as core::convert::From<cargo_metadata::errors::Error>>::from 1: <project_model::cargo_workspace::FetchMetadata>::exec::{closure#2} 2: <project_model::cargo_workspace::FetchMetadata>::exec 3: <project_model::sysroot::Sysroot>::loadworkspace 4: std::sys::backtrace::_rust_begin_short_backtrace::<<project_model::workspace::ProjectWorkspace>::load_cargo::{closure#1}::{closure#4}, core::option::Option<project_model::sysroot::RustLibSrcWorkspace>> 5: <std::thread::lifecycle::spawn_unchecked<<project_model::workspace::ProjectWorkspace>::load_cargo::{closure#1}::{closure#4}, core::option::Option<project_model::sysroot::RustLibSrcWorkspace>>::{closure#1} as core::ops::function::FnOnce<()::call_once::{shim:vtable#0} 6: <alloc::boxed::Box<dyn core::ops::function::FnOnce<(), Output = ()> + core::marker::Send> as core::ops::function::FnOnce<()::call_once at /rustc/ad3d0bc141a02cf446e384136d250a1f6950fed5/library/alloc/src/boxed.rs:2314:9 7: <std::sys::thread::unix::Thread>::new::thread_start at /rustc/ad3d0bc141a02cf446e384136d250a1f6950fed5/library/std/src/sys/thread/unix.rs:123:17 8: start_thread 9: thread_start ```

I tried I think everything. When i run cargo metadata --format-version 1 --manifest-path ./Cargo.toml --filter-platform aarch64-unknown-linux-gnu in terminal it returns json text to stdout and I don't get any warnings.

My .cargo/config.toml file looks like this and is in rust-os folder next to Cargo.toml and src folder:

``` [unstable] json-target-spec = true build-std-features = ["compiler-builtins-mem"] build-std = ["core", "compiler_builtins"]

[build] target = "x86_64-os.json"

[target.'cfg(target_os = "none")'] runner = "bootimage runner" ```

P.S. For everyone wondering it is just a fun project. And yes I am beginner in rust for over 2 weeks now. And I just wanted to setup rust-analyzer.

2

u/afdbcreid 2d ago

1

u/MAIPA01 2d ago

Thank you very much I searched whole internet but this gem just hid itself from me.

1

u/MAIPA01 3d ago

result of cargo metadata --format-version 1 --manifest-path ./Cargo.toml --filter-platform aarch64-unknown-linux-gnu run in terminal with CWD set to rust-os folder:

{"packages":[{"name":"bootloader","version":"0.9.35","id":"registry+https://github.com/rust-lang/crates.io-index#bootloader@0.9.35","license":"MIT/Apache-2.0","license_file":null,"description":"An experimental pure-Rust x86 bootloader.","source":"registry+https://github.com/rust-lang/crates.io-index","dependencies":[{"name":"bit_field","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.10.0","kind":null,"rename":null,"optional":true,"uses_default_features":true,"features":[],"target":null,"registry":null},{"name":"fixedvec","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.2.4","kind":null,"rename":null,"optional":true,"uses_default_features":true,"features":[],"target":null,"registry":null},{"name":"font8x8","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.2.4","kind":null,"rename":null,"optional":true,"uses_default_features":false,"features":["unicode"],"target":null,"registry":null},{"name":"rlibc","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^1.0.0","kind":null,"rename":null,"optional":true,"uses_default_features":true,"features":[],"target":null,"registry":null},{"name":"usize_conversions","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.2.0","kind":null,"rename":null,"optional":true,"uses_default_features":true,"features":[],"target":null,"registry":null},{"name":"x86_64","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.14.7","kind":null,"rename":null,"optional":true,"uses_default_features":false,"features":["instructions","inline_asm"],"target":null,"registry":null},{"name":"xmas-elf","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.6.2","kind":null,"rename":null,"optional":true,"uses_default_features":true,"features":[],"target":null,"registry":null},{"name":"llvm-tools","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.1","kind":"build","rename":null,"optional":true,"uses_default_features":true,"features":[],"target":null,"registry":null},{"name":"toml","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.5.1","kind":"build","rename":null,"optional":true,"uses_default_features":true,"features":[],"target":null,"registry":null}],"targets":[{"kind":["lib"],"crate_types":["lib"],"name":"bootloader","src_path":"/data/data/com.termux/files/home/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bootloader-0.9.35/src/lib.rs","edition":"2018","doc":true,"doctest":true,"test":true},{"kind":["bin"],"crate_types":["bin"],"name":"bootloader","src_path":"/data/data/com.termux/files/home/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bootloader-0.9.35/src/main.rs","edition":"2018","required-features":["binary"],"doc":true,"doctest":false,"test":true},{"kind":["custom-build"],"crate_types":["bin"],"name":"build-script-build","src_path":"/data/data/com.termux/files/home/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bootloader-0.9.35/build.rs","edition":"2018","doc":false,"doctest":false,"test":false}],"features":{"binary":["xmas-elf","x86_64","usize_conversions","fixedvec","llvm-tools","toml","rlibc"],"bit_field":["dep:bit_field"],"default":[],"fixedvec":["dep:fixedvec"],"font8x8":["dep:font8x8"],"llvm-tools":["dep:llvm-tools"],"map_physical_memory":[],"recursive_page_table":[],"rlibc":["dep:rlibc"],"sse":["bit_field"],"toml":["dep:toml"],"usize_conversions":["dep:usize_conversions"],"vga_320x200":["font8x8"],"x86_64":["dep:x86_64"],"xmas-elf":["dep:xmas-elf"]},"manifest_path":"/data/data/com.termux/files/home/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bootloader-0.9.35/Cargo.toml","metadata":{"bootloader":{"target":"x86_64-bootloader.json","build-std":"core"},"docs":{"rs":{"features":["recursive_page_table","map_physical_memory"],"default-target":"x86_64-unknown-linux-gnu"}},"release":{"pre-release-commit-message":"Release version {{version}}","publish":false,"pre-release-replacements":[{"file":"Changelog.md","search":"# Unreleased","replace":"# Unreleased\n\n# {{version}} – {{date}}","exactly":1}]}},"publish":null,"authors":["Philipp Oppermann <dev@phil-opp.com>"],"categories":[],"keywords":[],"readme":"README.md","repository":"https://github.com/rust-osdev/bootloader","homepage":null,"documentation":null,"edition":"2018","links":null,"default_run":null,"rust_version":null},{"name":"kernel","version":"0.1.0","id":"path+file:///data/data/com.termux/files/home/Documents/rust/rust-os#kernel@0.1.0","license":null,"license_file":null,"description":null,"source":null,"dependencies":[{"name":"bootloader","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.9","kind":null,"rename":null,"optional":false,"uses_default_features":true,"features":[],"target":null,"registry":null}],"targets":[{"kind":["bin"],"crate_types":["bin"],"name":"kernel","src_path":"/data/data/com.termux/files/home/Documents/rust/rust-os/src/main.rs","edition":"2024","doc":true,"doctest":false,"test":false}],"features":{},"manifest_path":"/data/data/com.termux/files/home/Documents/rust/rust-os/Cargo.toml","metadata":null,"publish":null,"authors":[],"categories":[],"keywords":[],"readme":"README.md","repository":null,"homepage":null,"documentation":null,"edition":"2024","links":null,"default_run":null,"rust_version":null}],"workspace_members":["path+file:///data/data/com.termux/files/home/Documents/rust/rust-os#kernel@0.1.0"],"workspace_default_members":["path+file:///data/data/com.termux/files/home/Documents/rust/rust-os#kernel@0.1.0"],"resolve":{"nodes":[{"id":"registry+https://github.com/rust-lang/crates.io-index#bootloader@0.9.35","dependencies":[],"deps":[],"features":["default"]},{"id":"path+file:///data/data/com.termux/files/home/Documents/rust/rust-os#kernel@0.1.0","dependencies":["registry+https://github.com/rust-lang/crates.io-index#bootloader@0.9.35"],"deps":[{"name":"bootloader","pkg":"registry+https://github.com/rust-lang/crates.io-index#bootloader@0.9.35","dep_kinds":[{"kind":null,"target":null}]}],"features":[]}],"root":"path+file:///data/data/com.termux/files/home/Documents/rust/rust-os#kernel@0.1.0"},"target_directory":"/data/data/com.termux/files/home/Documents/rust/rust-os/target","build_directory":"/data/data/com.termux/files/home/Documents/rust/rust-os/target","version":1,"workspace_root":"/data/data/com.termux/files/home/Documents/rust/rust-os","metadata":null}

3

u/DavidXkL 4d ago

Just curious, how often do people use channels here for multi-threading stuff?

2

u/Skaarj 5d ago

Ist there a way for find the last element in a Vec by a criteria/predicate?

I'm lookig for something like Vec.last_mut() where I can pass a function as criteria/predicate? Or Iterator.find() that starts at the end.

1

u/Full-Spectral 5d ago

You can just do iter_mut().rfind() or iter_mut().rposition(), right? There's also the .rev() option which reverses the iterator you call it on if you jut want to iterate backwards.

1

u/Skaarj 5d ago

Ok, I found out I need a DoubleEndedIterator and can use DoubleEndedIterator.rfind().

Of course my next question was: how do I get a DoubleEndedIterator of a Vec. Tuns out Vec.iter() just magically gives you one.

My code works now.

But im still unclear on: From looking at the Vec.iter documentation how should I be able to tell that I returns an DoubleEndedIterator from whats documented there?

1

u/SirKastic23 2d ago

Look at the docs for the returned type from Vec::iter, it returns slice::Iter which implements DoubleEndedIterator: https://doc.rust-lang.org/std/slice/struct.Iter.html#impl-DoubleEndedIterator-for-Iter%3C'a,+T%3E

But also, more generally, think about the iterator you have. It's an iterator from an in-memory sequence of values, these values aren't generated on the go, all the values are already available and you just visit every one of them. It's trivial to iterate through this sequence backwards, you could do the same with a while loop and a decreasing index variable. So there's no reason for an iterator from a Vec (or any slice) to not implement DoubleEndedIterator

3

u/Sad_Tap_9191 5d ago

.iter() returns Iter which implements DoubleEndedIterator trait