How do I split a string in Rust?

From the documentation, it's not clear. In Java you could use the split method like so: "some string 123 ffd".split("123");

How do I collect into an array?

I want to call .map() on an array of enums: enum Foo { Value(i32), Nothing, } fn main() { let bar = [1, 2, 3]; let foos = bar.iter().map(|x| Foo::Value(*x)).collect::<[Foo;...

How do I get an absolute value in Rust?

The docs are unhelpful: http://web.mit.edu/rust-lang_v0.9/doc/std/num/fn.abs.html Obviously, I can see the function right there, but I haven't got the faintest idea how to call it. Edit: The...

How to get a slice from an Iterator?

I started to use clippy as a linter. Sometimes, it shows this warning: writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used with non-Vec-based slices. Consider...

How can I test Rust methods that depend on environment variables?

I am building a library that interrogates its running environment to return values to the asking program. Sometimes as simple as pub fn func_name() -> Option<String> { match...

Is there a way to simplify converting an Option into a Result without a macro?

I have something like this (the real function is Ini::Section::get from rust-ini): impl Foo { pub fn get<K>(&'a mut self, key: &K) -> Option<&'a str> where K: Hash + Eq, { ...

How to emit LLVM-IR from Cargo

How can I get cargo to emit LLVM-IR instead of a binary for my project? I know that you can use the --emit=llvm-ir flag in rustc, but I've read some Github issues that show it's impossible to...

Does Cargo support custom profiles?

I often want to compile in release mode with debug = true so that I can read the generated assembly a bit easier. I am currently doing this: [profile.release] debug = true but I don't want any...

Generic cloneable/movable parameter as function argument

For an arbitrary struct that implements Clone, I would like to have a single generic function that takes either: a &MyStruct in which case it can be conditionally cloned by the function a...

Using the index from enumerate outside the loop block

Consider this example of solving Advent of Code 2015 1.2. fn main() { // advent of code 1.2 2015 // you are at floor 0 // if instruction is ) go one floor up, else go one floor down ...

How to initialize a static array with most values the same but some values different?

I would like to use a static or const array, but initialize it using something other than the [T; N] syntax. I need to define specific elements but all other values can default to 0 or some other...

linker `cc` not found,

After installing First time rust on ubuntu try to run this program. fn main() { println!("Hello, world!"); } Error: $ cargo run Compiling test1 v0.1.0...

Rust: How to take input from a file and compute it?

I just need to find a way to work with input to do simple maths with it. I've been trying this for a couple of days for the advent of code 2019 day 1 use std::fs::File; use std::io::{BufRead,...

Why does Rust need the PartialOrd trait in addition to the Ord trait?

I want to define Ord for a custom type Point so it is sorted by the distance to the origin for Advent of Code 2019 day 10: impl std::cmp::Ord for Point { fn cmp(&self, other: &Self) ->...

How to get the size of a struct field in Rust without instantiating it

I have a structure with a byte array in it. This structure actually comes from the FFI bindings produced by bindgen, and its size is defined in C code using a macro, i.e.: C code: #define FOO_SIZE...

How do I make a string outlive a closure body?

I have this code: .and_then(move |key: Option<String>| async { let pool = pool.clone(); let key = key.as_ref().map(|s| &**s); match pool.get() { ...

How to call Rust async method from Python?

I want to use a Rust async method in Python. I'm trying to use PyO3 or rust-cpython. For example, for sync Rust functions, I can use, #[pyfunction] fn myfunc(a: String) -> PyResult<String> { ...

Clippy says `too many arguments` to static declaration

This code is from my OS. #[global_allocator] pub static ALLOCATOR: LockedHeap = LockedHeap::empty(); Clippy says this function has too many arguments. error: this function has too many arguments...

Declare char for comparison

As part of Advent of Code 2020 day 3, I'm trying to compare characters in a string to a specific character. fn main(){ let str_foo = "...##..# ##.#..#."; for char in str_foo.chars() { ...

How to construct a HashMap with boxed Fn values

I'm relatively new to using Rust, and I'm using it for Advent of Code to help me learn. For the fourth problem, I want to create a lookup table using a HashMap to map from string keys to function...

Mysterious error "one type is more general than the other" when using Box with nom parsers

While trying to solve today's Advent of Code puzzle, I am attempting to use nom to build some dynamic parsers depending on input strings. But in doing so I've come against what to me is a very...

Rust error expected type `()` found type `(i32, i32)`

Pretty new to Rust, decided to brush up using the Advent of Code 2020 Day 1 Puzzle. I'm using the following function: fn find_numbers_2020(v: Vec<i32>) -> (i32,i32) { let mut n1: i32 = 0; ...

How to define a Vec of Enums as a field in rust sqlx model

I am trying to load an array field in Postgres to a Rust struct as follows use sqlx::{Pool, PgConnection, PgPool, FromRow}; use sqlx::postgres::PgQueryAs; #[derive(Copy, Clone,...

Cannot compile Rust project, linking with `cc` failed

I forked a repository and modified it a bit. The crate compiles by its own, and all the tests pass. However, I cannot get it to build when I attempt to use it in another project. The package is at...

How to get collection of document from mongodb cursor?

I have the following code which should return a list of documents from mongodb. struct Vehicle{ id: String, name: String } pub async fn list_all() -> Vec<Vehicle>{ let mongodb =...

Rust, OpenSSL, solana-test-validator library not loaded issue

I am on Mac(M1) Big Sur 11.4, Homebrew version 3.2, [email protected] I try to run solanar-test-validator in their README , I install open ssl with homebrew then I linked the openssl sudo ln -s...

How to download another private repository in Github Actions with Cargo?

Problem I have a private Rust project (A) and it depends on another private Rust project (B). On my local machine, it works because I am logged into git. I am not sure how to login into git in...

next failed to load SWC binary

When trying to run the command using nextjs npm run dev shows error - failed to load SWC binary see more info here: https://nextjs.org/docs/messages/failed-loading-swc I've tried uninstalling node...

Iterate mutably over &[1,2,3,4,5,6,7,8,9] to receive `&mut [1,4,7]` then `&mut [2,5,8]` then `&mut[3,6,9]`

I'm trying to iterate over a slice broken into chunks, and return a tuple with the nth element of each chunk. Example: &[1,2,3,4,5,6,7,8,9] I'd like to break this into chunks of size 3, and then...

How do I chain operators over lists in rust? Looking for equivalent to kotlin code

I have the following code in kotlin and I'm trying to find a rust equivalent, but don't understand the chaining mechanism in rust to convert. val windowSize = 2 val result = listOf(1, 2, 3, 4, 5,...