Hot answers tagged

34 votes
Accepted

Calculate mean, median, and mode in Rust

Learn to love rustfmt. For example, the Rust standard is 4-space indents. Learn to love Clippy, which can show you the more idiomatic way to iterate over a collection: ...
user avatar
  • 8,308
26 votes
Accepted

Simplest way to write "FizzBuzz" in Rust

I think that match is better, you just do not know how to cook it ;) ...
user avatar
  • 276
23 votes
Accepted

Wanted a cat. Got lolcat

C would have allowed you to just ignore errors and pretend everything was good. A naïve cat would be very short, but rather unreliable also. A well‐written ...
user avatar
23 votes
Accepted

Web crawler in Rust

Cargo Fmt There's a very common tool accessible through cargo which can format all of the code in your project according to Rust's official style guide. Many major ...
user avatar
16 votes
Accepted

Simple cat in Rust

imports Your imports should be compressed: use std::io::{self, Read, Write}; Although it might be better to use the io ...
user avatar
  • 9,533
16 votes
Accepted

Shoe shine shop model in Rust

Correctness of the solution On reflection, I'm not sure either the C++ or the Rust code solves the problem as stated. I'm not completely sure I understand the shoe shine shop model so I may be wrong. ...
user avatar
  • 1,034
15 votes

How can I find out why this Rust program to echo lines from stdin is slow?

I'm going to treat this as a code review question where the goal is to figure out how to write high-performance I/O code in Rust. There are two critical steps to speed up Rust I/O: Make sure the ...
user avatar
  • 251
14 votes
Accepted

Binary Tree Implementation in Rust

Examples have first-class support in Cargo. Place them in a directory examples next to src. They can then be run with ...
user avatar
  • 8,308
12 votes
Accepted

Fahrenheit and Celsius converter in Rust

The first thing I always do is running clippy. You will catch some things that are not neccessary, e.g. fn main() -> () ...
user avatar
  • 436
12 votes
Accepted

Circular FIFO Buffer

Your code looks pretty decent. My two cents: Implement Default ...
user avatar
  • 436
12 votes
Accepted

Writing slice compare in a more compact way

What about this: ...
user avatar
  • 869
11 votes
Accepted

Extracting the last component (basename) of a filesystem path

Firstly, you should use rsplit and next rather than split and ...
user avatar
11 votes
Accepted

K-Means in Rust

I took your code and got it to compile with my version of Rust (rustc 0.13.0-dev (29ad8539b 2014-12-24 16:21:23 +0000)). I ran with your parameters (I hope I ...
user avatar
  • 8,308
11 votes
Accepted

Insertion sort in Rust

As you have already been made aware, you should not be using &mut Vec<T> unless you plan on adding or removing items from the ...
user avatar
  • 8,308
11 votes
Accepted

n-th Fibonacci number with memoization

I like this. Your code is clean and pretty readable. I recommend using rustfmt, if only because it will stop annoying people on the internet from recommending you ...
user avatar
  • 1,034
10 votes
Accepted

Shift and merge numbers as in 2048 game code

With both scoring and merging Oops! I did indeed neglect to include merging. This is my mistake; I'm not a big fan of returning values via mutable parameters, so I think my brain shut off. ...
user avatar
  • 8,308
10 votes
Accepted

Gamma function in Rust

There's not a lot of code here so there's not a lot to say. ^_^ Implementations like this are generally outside of the day-to-day knowledge of most programmers. You may wish to include a reference in ...
user avatar
  • 8,308
10 votes
Accepted

Pig Latin exercise in Rust

For overall issues, these jump out: s[i].is_alphabetic() && i < s.len() can panic, because you access the value before checking the length, it would ...
user avatar
10 votes

Shoe shine shop model in Rust

Forgive me, I am unable to review the rust code because I do not know rust, I am only reviewing the c++ code.. Use System Defined Exit Codes Returning -1 as an ...
user avatar
  • 18.5k
10 votes
Accepted

Number-guessing game based on Rust tutorial

Welcome to Rust! Code Formatting There are some inconsistencies in your formatting — I have run cargo fmt to get rid of them. Naming There's no need to prefix ...
user avatar
  • 8,987
9 votes

Rust Brainfuck interpreter

Profile You can only improve what you can measure. So first of all let us run callgrind to check where we spent most of our time: ...
user avatar
  • 18.8k
9 votes
Accepted

A Rusty implementation of CowSay

The tests fail. That's not a good thing: ...
user avatar
  • 8,308
9 votes
Accepted

BTreeMap as a multimap in Rust

Idiomatic Rust style is to place braces on same line. When the function definition gets longer than one line then the brace lives on the next line all on its own. Learn and love the ...
user avatar
  • 8,308
8 votes
Accepted

Tokenize s-expressions in Rust

The biggest thing I can say is listen to your tests. The very first thing I noticed as I was skimming is how repetitive they were. Even worse, the repeated parts drowned out the interesting aspects of ...
user avatar
  • 8,308
8 votes

Selection sort algorithm in Rust

You could use min_by_key to find the minimum's index and the method swap defined in slices: ...
user avatar
  • 2,974
8 votes
Accepted

MergeSort in Rust

Disclaimer: I'm also new to Rust, but I have a background in C, C++ and Haskell. Take everything I say with a grain of salt. All of that looks reasonable, except for the ...
user avatar
  • 18.8k
8 votes
Accepted

Alphabet cypher in Rust

Disclaimer: I'm a Rust beginner with a background in C and C++. Now that I've lost my credibility, let's have a look at your code. Use rustfmt, clippy and rustc There are (at least) three helpful ...
user avatar
  • 18.8k
8 votes
Accepted

Recursive Fibonacci in Rust with memoization

There's no reason to ascribe a type to memo. Don't expose the memoization logic outside the call. Instead, create a shim function that creates the memoization ...
user avatar
  • 8,308
8 votes

Rust Torrent Parser

For deserialization, its a lost easier if you use serde. Then you can do something like this: ...
user avatar
8 votes

Writing slice compare in a more compact way

I take the idea to return an Ordering from the other answer: ...
user avatar
  • 590

Only top scored, non community-wiki answers of a minimum length are eligible