Read the gzip answer with Hyper and Flate2

Hyper has the function fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> to read the contents of an HTTP response into the provided &mut [u8]. Flate2 can gunzip: let mut d = GzDecoder::new("...".as_bytes()).unwrap(); l

How to freeze one thread and notify another?

I need to pause the current thread in Rust and notify it from another thread. In Java I would write: synchronized(myThread) { myThread.wait(); } and from the second thread (to resume main thread): synchronized(myThread){ myThread.notify(); } Is is po

Concatenate slices of table

I have two (very large) arrays foo and bar of the same type. To be able to write some nice code, I would like to obtain a read-only slice, result, of the concatenation of the two arrays. This operation must run in O(1) time and space. Array access fo

How to obtain a duration in milliseconds in Rust

I have a time::Duration. How can I get the number of milliseconds represented by this duration as an integer? There used to be a num_milliseconds() function, but it is no longer available.Here is the solution I came up with, which is to multiply the

Should we dereference the image before it is associated?

As a newcomer to Rust, I've stumbled upon two apparently valid ways of running a match on a reference type. Let's say I've defined enum Color { Red, Yellow, Green, Teal, Blue, Purple }, and I want to implement a function that works on a &self referen

UDP API in Rust

1) API for send here returns Result<usize>. Why is that ? In my head, a UDP send is all or none. The return value seems to suggest that sending can succeed but entire data may not be written which makes me code like: let mut bytes_written = 0; while

Work with line objects that require a size

I'd like to have a LinkedList of trait object wrapper structs. The inner would be a stream type for either an Ssl or Non-Ssl stream. My hope was to pass the struct wrapper around, and as long as the inner conformed to the same trait, everything would

Extract substrings effectively from prefixes

Currently I'm using the following function to extract prefix substrings: fn prefix(s: &String, k: usize) -> String { s.chars().take(k).collect::<String>() } This can then be used for comparisons like so: let my_string = "ACGT".to_st

How can I wrap another type and add fields and methods?

I know inheritance is a bad word in the Rust world, however there must be some way, given the following struct struct Glyph { // Fields } to have a new Glyphs struct that is a wrapper for Vec<Glyph> methods (push, etc.) as well as fields and methods

Why try? () And? not compiled when used in the main?

Why does this code not compile? use std::io; use std::fs; use std::path::Path; fn main() { // Open path let dir = Path::new("../FileSystem"); // Check if it is a directory if !dir.is_dir() { println!("Is not a directory"); return; } fo

How can I ask Cargo to recompile automatically modified files?

I have heard cargo has the ability to automatically recompile changed source files, but I'm having a hard time figuring out how to tell it to do so. For now, I am manually running cargo build or cargo run every time I want to type check my code. I wo

Why does not Rust support updating character objects?

Given this code: trait Base { fn a(&self); fn b(&self); fn c(&self); fn d(&self); } trait Derived : Base { fn e(&self); fn f(&self); fn g(&self); } struct S; impl Derived for S { fn e(&self) {} fn f(&self) {} fn g(&

How to constrain the element type of an iterator?

I'm converting some older Rust code to work on 1.0.0. I need to convert a function that takes an iterator over characters, which used to be written like this: fn f<I: Iterator<char>>(char_iter: I) Now that Iterator doesn't take a parameter, th

Moving property borrowed in a mutable way

My understanding is that a mutable borrower can move ownership to another mutable borrower. But this move seems to be a bit different from moving a non-pointer variable. Let's see an example. Below p1 gets moved to p2 when compute() is called the fir

How do you use DynamicLibrary with a DLL on Windows in Rust?

I've been trying to get a basic proof of concept up and running using cmake and rust here, but to no avail. The basic idea is to build a DLL and then load symbols like this: let dl = DynamicLibrary::open(

Iterate on the content of an option or on a specific value

Let's say that we have the following C-code (assume that srclen == dstlen and the length is divisible by 64). void stream(uint8_t *dst, uint8_t *src, size_t dstlen) { int i; uint8_t block[64]; while (dstlen > 64) { some_function_that_initializes_bloc

Access immutable methods during iteration on an editable member

While learning about iterators in Rust, I created the following struct to hide the implementation of a two-dimensional collection: use std::slice::{Items, MutItems}; use std::vec::{Vec}; pub struct Table<T> { pub width: uint, pub height: uint, data:

& ldquo; toggle & rdquo; through enum variants

Let's say we have an enum enum Direction { NORTH, SOUTH, EAST, WEST } I'd like to write a function which toggle/switches the provided value to the next in the enum (and wrap around too). e.g. NORTH => SOUTH, SOUTH => EAST.....WEST => NORTH Questi

Ownership rules for rust in the context of closures

I'm playing with the new closures in Rust and I ran into a situation that again showed me I didn't fully grasp the ownership rules yet. #![feature(unboxed_closures)] #![feature(overloaded_calls)] fn main() { let mut test = 5i; let do_something = ref

Index a Vec of Box error

I was playing around with the Vec struct and ran into some interesting errors and behavior that I can't quite understand. Consider the following code. fn main() { let v = vec![box 1i]; let f = v[0]; } When evaluated in the rust playpen, the code prod