Rust is a systems programming language without a garbage collector focused on three goals: safety, speed, and concurrency. Use this tag for questions about code written in Rust. Use an edition specific tag for questions that refer to code which requires a particular edition, like [rust-2018]. Use more specific tags for subtopics like [rust-cargo] and [rust-macros].
2,911
questions with no upvoted or accepted answers
I am making a simplistic mark-and-compact garbage collector. Without going too much into details, the API it exposes is like this:
/// Describes the internal structure of a managed object.
pub struct ...
asked May 20 '15 at 5:18
pyon
15.7k1414 gold badges8383 silver badges144144 bronze badges
trait A<'self_>: 'self_ {
type I;
}
trait AMut
where
Self: for<'self_> A<'self_>,
for<'self_> <Self as A<'self_>>::I: 'static,
{
fn mutate_self(&...
asked Dec 6 '15 at 4:29
user
3,69233 gold badges2222 silver badges3333 bronze badges
I have a strange piece of code:
#![allow(unused)]
fn f<'a>() {}
fn g<'a: 'a>() {}
fn main() {
// let pf = f::<'static> as fn(); // (7)
let pg = g::<'static> as fn(); ...
I've set up the Visual Studio Code debugger and run the following program.
pub fn main() {
let mut chars = "test".chars();
match chars.next() {
Some(c) => {
println!("...
I am studying the implementation of Seqlock. However all sources I found implement them differently.
Linux Kernel
Linux kernel implements it like this:
static inline unsigned __read_seqcount_begin(...
asked Jun 2 '19 at 23:30
lz96
2,37111 gold badge2020 silver badges4141 bronze badges
I'm writing a function that accepts different trait implementors. One of them is a closure. Some closures need an argument type annotation and some don't, depending on their bodies.
Example (...
asked Feb 28 '19 at 15:34
How do I send an HTTP request using a proxy with Hyper 0.11? I have the following working code that sends an HTTP request without proxy:
extern crate hyper;
extern crate tokio_core;
extern crate ...
asked Jul 26 '17 at 15:00
During the 1.12 beta, I built and ran code this for Android without problems:
[package]
name = "android"
version = "0.1.0"
authors = ["Author <mail@email.com>"]
build = "build.rs"
[lib]
name = ...
asked Oct 31 '16 at 7:02
fghj
7,55033 gold badges2222 silver badges4848 bronze badges
I stumbled upon this question, and having read ramslök's comment on the accepted answer, I tried it out with a type that was not Copy.
To my surprise it compiled successfully, but it's destructor ...
asked Jul 26 '20 at 19:01
I'm building a library in Rust that will be called from C/C++ code. Cargo.toml is configured to output the crate as a static library:
[lib]
crate-type = ["staticlib"]
I have a test in tests/...
asked Dec 20 '16 at 11:44
Are there any general rules, design documentation or something similar that explains how the Rust standard library deals with threads that were not spawned by std::thread?
I have a cdylib crate and ...
asked Nov 21 '16 at 17:37
fghj
7,55033 gold badges2222 silver badges4848 bronze badges
It may be not a good idea or not idiomatic, but let's assume that for some reason a library relies on catch_unwind for its business logic.
Can I somehow warn (by failing the compilation with an error ...
asked Aug 15 '18 at 14:28
ozkriff
9391010 silver badges1919 bronze badges
I am currently trying to wrap my head around the tokio & futures primitives and ecosystem.
I started doing some work from the tk-http websockets example, and wanted to do more processing on the ...
asked Jul 8 '17 at 19:56
remram
3,5892929 silver badges4141 bronze badges
Given the type std::cell::Ref, it seems to be basically a pointer. Yet it doesn't have Hash, PartialEq or Eq trait implementations. Is there any fundamental reasons these traits haven't been added?
asked Mar 22 at 19:55
Neil Mitchell
8,56311 gold badge2424 silver badges7575 bronze badges
I was playing with zero-sized types (ZSTs) as I was curious about how they are actually implemented under the hood. Given that ZSTs do not require any space in memory and taking a raw pointer is a ...
asked Oct 23 '19 at 19:40
Danila Kiver
2,10611 gold badge1212 silver badges2222 bronze badges
Background: I made a silly code generation program for various programming languages, the goal being to compare how long it takes various compilers to compile absurdly long 100K+ LOC programs made of ...
asked Dec 9 '18 at 19:49
Aaron B
61988 silver badges1111 bronze badges
If I want to create a Cartesian product of a list of lists in Haskell, I can do this:
product [] = [[]]
product (xs:xss) = concatMap (\k -> map (k:) (product1 xss)) xs
or even this:
sequence xss
...
asked Dec 6 '17 at 7:22
user1685095
4,98977 gold badges3838 silver badges8484 bronze badges
I'm using Visual Studio Code and the LLDB Debugger (CodeLLDB vadimcn.vscode-lldb) for programming in Rust. When using Vec<u64> I can see all values in a list (until a limit of > 10000):
When ...
asked Jun 29 '20 at 12:44
kaiser
77511 gold badge77 silver badges1919 bronze badges
LLVM appears to ignore core::intrinsics::assume(..) calls. They do end up in the bytecode, but don't change the resulting machine code. For example take the following (nonsensical) code:
pub fn one(...
asked Jan 15 '20 at 12:04
llogiq
11.7k33 gold badges3636 silver badges6464 bronze badges
I have an existential type defined like this:
trait Collection {
type Element;
}
impl<T> Collection for Vec<T> {
type Element = T;
}
type Existential<T> = impl Collection&...
asked Apr 24 '19 at 9:25
Tim Diekmann
5,69099 gold badges2929 silver badges5353 bronze badges
I have a Rust executable which was compiled purely within the Rust ecosystem; no external C code or linked libraries, sans whatever the compiler drags in.
After compiling it with ASAN on Linux, ASAN ...
asked Feb 8 '18 at 7:12
user
3,69233 gold badges2222 silver badges3333 bronze badges
I have this dependency:
blas = "0.18.1"
During building this simple program (no types used de facto in the program):
extern crate blas;
use blas::c::*;
I get these errors:
error: linking with `...
Here's a simple n-th prime program written in Rust. The implementation is inefficient, but that's not the point.
use std::time::Instant;
fn main() {
let n = 10_000;
let now = Instant::now();
...
I created a repository with a Node.js based and a Rocket.rs based web server inside.
Consider these steps:
Start the Rocket.rs server via cargo run --release
Start the Node.js server via node server....
asked Feb 3 at 22:26
Attila Kun
1,92522 gold badges2121 silver badges3232 bronze badges
When I was trying to generate a string, I used both format! and directly pushed into a String.
Pushing into a String directly took ~40 ns/iter while using format! took ~80 ns/iter (twice as long).
Why ...
String implements Deref<Target = str>, which means that the following code compiles:
fn save(who: &str) {
println!("I'll save you, {}!", who);
}
save(&String::from("Madoka"));
If ...
asked Dec 24 '18 at 1:24
Lambda Fairy
11.4k44 gold badges3737 silver badges5656 bronze badges
Rust has a very good lint interface, divided into EarlyLintPass and LateLintPass trait objects. Unfortunately, even the early passes don't see #[cfg] or #[cfg_attr] attributes, and thus cannot lint ...
asked Oct 24 '18 at 16:13
llogiq
11.7k33 gold badges3636 silver badges6464 bronze badges
I tried to compile one of my Rust projects to the x86_64-unknown-linux-gnu target:
$ cargo build --target=x86_64-unknown-linux-gnu
Compiling deployer v0.1.0 (file:///Users/raphael/web/deployer)
...
asked Nov 4 '16 at 13:44
rap-2-h
22.8k2323 gold badges128128 silver badges217217 bronze badges
As a concrete example, can I add a template for Rust that will allow me to develop, compile, and debug the application from inside Xcode?
My guess is that I would need to build a new Project Template ...
asked Mar 24 '16 at 12:23
When using serde in stable as recommended by the respective blog post, one will have to use the built-in include! macro to pull in a file generated by serde-codegen.
The file linked here shows this ...
asked Jun 11 '15 at 10:10
Byron
3,16311 gold badge2121 silver badges3232 bronze badges
I have to deserialize JSON blobs where in some places the absence of an entire object is encoded as an object with the same structure but all of its fields set to default values (empty strings and ...
asked Oct 2 '18 at 15:10
main--
3,6721313 silver badges3535 bronze badges
I've got two values: n: f64 and p: i32, and I need to compute n * 10^p.
I tried two methods:
Using multiplication and f64::powi
Using format!() and f64::from_str
The latter is more accurate (see ...
asked Dec 26 '20 at 12:44
Dogbert
188k3939 gold badges334334 silver badges347347 bronze badges
My laptop doesn't have a dedicated GPU. I run elementary OS 5 on this laptop, which has an integrated GPU. When I try running my beginner's Bevy app,
use bevy::prelude::*;
fn main() {
App::build()...
My goal is to get a .a static library on linux from the MediaPipe project, which is built with Bazel. To my knowledge, there is no bazel rule for doing so. I really don't want to integrate with Bazel -...
I have a struct which implements Deserialize and uses the serde(deserialize_with) on a field:
#[derive(Debug, Deserialize)]
struct Record {
name: String,
#[serde(deserialize_with = "...
asked Jul 11 '19 at 8:44
pixunil
29122 silver badges1313 bronze badges
This is likely a textbook case of me not understanding some of the technicalities of the borrow checker, but it would be nice if someone could clear this up for me.
I have this (incredibly simplified) ...
asked Jan 30 at 19:19
Silvio Mayolo
22.3k33 gold badges3232 silver badges6363 bronze badges
Suppose you have a dependency called "dep" which has two features called f1 and f2. I want to use "dep" with the f1 feature when I'm building my crate normally, but use it with f2 ...
asked Oct 20 '20 at 3:41
Alex Sed
52044 silver badges88 bronze badges
There are tons of questions about self-referential structs in Rust here, and I think I've read them all, but I'm still having trouble wrapping my head around things. What kind of design patterns exist ...
asked Nov 29 '19 at 20:23
Peter
30511 gold badge44 silver badges1414 bronze badges
My Rust project no longer builds after some conflicts between my RLS plugin and terminal build. I searched around the web and found suggestion of removing my ~/.cargo/registry/index/*, but after that ...
asked Nov 18 '18 at 12:49
Fei
1,1481414 silver badges2727 bronze badges
I have this code, for which borrow checker shows error:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=55d050b6f25410ce0e17ef9e844b048d
fn f1(v: &str) {
}
fn ...
asked Sep 12 '20 at 1:31
rsk
97411 gold badge1010 silver badges1717 bronze badges
I find I am using this pattern a lot.
Arc<Mutex<dyn SomeTrait + Send>>;
and so I thought I would do this:
pub type NicePtr<T> = Arc<Mutex<dyn T + Send>>;
but this does ...
asked Aug 22 '20 at 1:27
pm100
32.3k1919 gold badges6969 silver badges122122 bronze badges
The ctor crate doesn't support web assembly currently, although there is active discussion about how to fix this.
Although I am well aware of the issues associated with static initialization coming ...
asked Jul 11 '20 at 21:24
Joseph Garvin
18.6k1717 gold badges8383 silver badges150150 bronze badges
I am writing a Rust project with several libraries. Some of the libraries export types that are consumed by other libraries in the workspace. In addition to the Rust crates, I would also like to ...
asked Jul 1 '20 at 23:41
bnaecker
5,07411 gold badge1010 silver badges2929 bronze badges
I'm quite new with Rust, and my first 'serious' project has involved writing a Python wrapper for a small Rust library using PyO3. This has mostly been quite painless, but I'm struggling to work out ...
asked May 22 '20 at 12:37
thesketh
17111 silver badge11 bronze badge
So i struggle for some time because the Rust Compiler won't give me sufficient information about this Problem.
I try to compile a Rust lib into a wasm module with wasm-pack build --out-dir [dir] but ...
I use rust 1.34 and 1.35. Currently it links to GLIBC_2.18.
How can I limit cargo build to link GLIBC up to version 2.14?
asked Sep 1 '19 at 20:07
Yixing Liu
1,58911 gold badge1212 silver badges2929 bronze badges
I have macro with return statement like this:
macro_rules! return_fail {
( $res:expr ) => {
match $res {
Ok(val) => val,
Err(e) => {
...
asked Sep 8 '20 at 17:34
너를 속였다
43244 silver badges1313 bronze badges
I was studying the x86_64 assembly for the following function:
/// Returns the greatest power of two less than or equal to `self`, or 0 otherwise.
pub const fn prev_power_of_two(n: u64) -> u64 {
...
asked Mar 16 at 23:47
orlp
97.5k2929 gold badges187187 silver badges284284 bronze badges
Consider the following rust program:
fn f<'a>(x: &'a i32) {
unimplemented!();
}
fn main() {
f::<'static>;
}
When compiling it, the following compilation error is outputed:
...
asked Feb 21 at 12:57
Supremum
39477 silver badges2222 bronze badges
In a happy world libraries don't spawn futures themselves or heavily depend on tokio/async-std/etc.. right? But how to(at least temporarily) work around the need to mix two executors? I'm using Riker, ...
asked Jul 24 '20 at 10:21
olanod
29.2k77 gold badges3939 silver badges7373 bronze badges