Questions tagged [rust]

Rust is a language designed for writing highly reliable and fast software in a simple way. It can be used from high-level code down to hardware-specific code, and from big irons to tiny devices.

0
votes
0answers
13 views

Compiler thinks that “borrowed value does not live long enough”, but it does

I have a problem where I have a local variable that holds an owned value. I pass a reference of it to a method, which creates a new object and stores the reference inside it. After calling that method ...
0
votes
1answer
28 views

Visitor pattern in Rust

I found an interesting implementation of the visitor pattern in Rust on GitHub: pub trait Visitor<T> { fn visit(&mut self, t:&T); } pub trait Visitable: Sized { fn accept<T&...
-2
votes
1answer
49 views

How to call a T -> T function on a &mut T value

Suppose I have fn f(x: T) -> T and a value p: &mut T. Is there some trick for writing *p = f(*p)? I have a solution using mem::replace that works if T has a default value, and I can generalize ...
1
vote
0answers
42 views

how to call c function from rust

I have two files c.c and r.rs. r.rs contains the main function and it calls a function from c.c c.c:- int testing(int i){ return i*2; } r.rs:- extern "C" { fn testing(x:i8) -> i8; } fn ...
3
votes
0answers
29 views

Why does this deref coercion fail when the expression is wrapped in a block?

String implements Deref<Target=str>. This means that the following code compiles: fn save(who: &str) { println!("I'll save you, {}!", who); } save(&String::from("Madoka")); If I ...
1
vote
1answer
40 views

cannot return reference to temporary value where the value have static lifetime

Code pub struct StructType { a: &'static str, } pub trait Foo { fn foo() -> &'static [&'static StructType]; fn bar() -> &'static StructType; } pub struct Test; ...
1
vote
2answers
39 views

Define a generic function that takes a Mutex<T>

This simple function locks a f64 and updates the value use std::sync::Mutex; fn bar() { let a = Mutex::new(1.0); let mut b = a.lock().unwrap(); *b = 2.0; foo(a, 3.0); } I would like ...
1
vote
0answers
35 views

How to close a modified and executing `futures::sync::mpsc::Receiver` stream?

I'd like to be able to do something along these lines: let (tx, rx) = futures::sync::mpsc::channel(1000); let arc = Arc::new(Mutex::<Option<AndThen<Receiver<u32>, _, _>>>::...
-2
votes
1answer
53 views

speed decrease for loop rust

I am teaching myself rust and my background is in python. Rust is my first compiled language that I'm learning so I figured I'd have a go at it while doing some problems on project euler. This ...
2
votes
4answers
129 views

Idiomatic way to get the index of max float value in a Vec in Rust

Assumption -- The Vec<f32> does not have any NaN values or exhibit any NaN behavior. Take the following sample set: 0.28 0.3102 0.9856 0.3679 0.3697 0.46 0.4311 0.9781 0.9891 0.5052 0.9173 ...
0
votes
3answers
93 views

What does Some() do on the left hand side of a variable assignment?

I was reading some Rust code and I came across this line if let Some(path) = env::args().nth(1) { Inside of this function fn main() { if let Some(path) = env::args().nth(1) { // Try ...
0
votes
1answer
55 views

Why can I not access this Rust simple server from the Internet?

I have the following server: extern crate simple_server; use crate::simple_server::*; fn main() { let host = "127.0.0.1"; let port = "7878"; let server = Server::new(|request, mut ...
-1
votes
0answers
29 views

I don't want to generate json string include `null` and | or default value of the primary type when serializing

The serde_json::to_string() function will generate a string which may inlclue null value for Option<T> type, or 0 for u32 type, That make the output larger, so I want to ignore those sort of ...
0
votes
1answer
48 views

Matching string in Rust [duplicate]

I'm new to Rust (1.31) and I would like to understand a simple piece of code that does not compile: fn main() { s = String::from("foo"); match s { "foo" => { println!("...
-2
votes
1answer
38 views

Stop Rust from enforcing the serde::Deserialize trait on an error type

The code below is the beginnings of a small library I'm writing to talk to a web API. Users of the library will instantiate a client MyClient and access the web API through it. Here, I'm trying to get ...

15 30 50 per page