# Getting started in Rust

**URL:** <https://metaruby.com/t/getting-started-in-rust/375>\
**Category:** General Programming\
**Created:** [October 18, 2015, 9:57pm UTC](https://metaruby.com/t/getting-started-in-rust/375 "2015-10-18T21:57:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 18, 2015, 9:57pm UTC](https://metaruby.com/t/getting-started-in-rust/375/1 "2015-10-18T21:57:26Z")

</div>

I’m starting to learn Rust! This would make an excellent place to learn it! This is my first “program”. All it does is take user input, matches all the integers via regex. Converts the strings to integers, multiply them by 2, and append them on a vector. Then print out the results.

Here’s the dependency

```rust
[dependencies]
regex = "0.1.41"

```

And the script

```rust
extern crate regex;

use std::io;
use regex::Regex;

fn main() {
  println!("It's Rust!");

  let mut guess = String::new();

  io::stdin().read_line(&mut guess)
    .ok()
    .expect("Failed to read line");

  let re = Regex::new(r"(\d+)").unwrap();

  let mut results: Vec<i32> = vec![];

  for cap in re.captures_iter(&guess) {
    let mut val: i32 = cap.at(0).unwrap().parse::<i32>().unwrap();
    val *= 2;
    results.push(val)
  };
  
  println!("{:?}", results);
}

```

* * *

**My thoughts/discoveries during the process**

I used Regex because converting a String to an Array looked like it would be very difficult to figure out. I used a Vector for the new Array because it’s not a fixed size and I can used an undetermined amount of input for it. One thing I found is that when you have a method return a result you get an object wrapper of a result (I believe this to be true, I could be wrong), and each item you return you need to **unwrap()** to get the raw type result. Parameters preceded with an ampersand are “borrowed” references (as in - they don’t take ownership of the object/memory).

**Example output (and input)**

```plaintext
It's Rust!
3,4,5
[6, 8, 10]

```

**Getting started**

[Install Rust](https://www.rust-lang.org/). Use **cargo new project\_name --bin** to generate a program directory. Add any dependencies in the **.toml** file. And edit the **src/main.rs** with your code. When you’re ready run **cargo build** , and if you have no errors then run **cargo run**. If you do see errors you will have a very detailed explanation of what’s wrong.

To get a good idea of how Rust works watch this presentation:

[![](https://metaruby.com/uploads/default/original/2X/1/181c066432a3e49dcf919eb526855069ebb11419.jpeg "Intro to the Rust programming language") ](https://www.youtube.com/watch?v=agzf6ftEsLU)

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 19, 2015, 3:12am UTC](https://metaruby.com/t/getting-started-in-rust/375/3 "2015-10-19T03:12:22Z")

</div>

If you use Vim I suggest you add these two Vim plugins to your .vimrc file.

[http://github.com/rust-lang/rust.vim](http://github.com/rust-lang/rust.vim) - syntax highlighting  
[http://github.com/racer-rust/vim-racer](http://github.com/racer-rust/vim-racer) - auto-completion

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [October 19, 2015, 1:38pm UTC](https://metaruby.com/t/getting-started-in-rust/375/4 "2015-10-19T13:38:48Z")

</div>

What made you want to learn Rust, Dan?

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 19, 2015, 2:09pm UTC](https://metaruby.com/t/getting-started-in-rust/375/5 "2015-10-19T14:09:57Z")

</div>

This blog post about writing Ruby methods in Rust [Rust to the rescue (of Ruby)](https://medium.com/@fbzga/rust-to-the-rescue-of-ruby-2067f5e1dc25) by [Fabiano B.](https://medium.com/@fbzga). I’ve read one other post like it and I’ve seen Yehuda Katz do it in a conference talk.

**GoGaRuCo 2014- Let’s Talk About Rust by Yehuda Katz**

[![](https://metaruby.com/uploads/default/original/2X/2/24c6780b25d24cfcaba031dd3dd33a097baa492f.jpeg "GoGaRuCo 2014- Let’s Talk About Rust") ](https://www.youtube.com/watch?v=ySW6Yk_DerY)

Also notably Yehuda Katz; who’s one of the authors of Ruby’s Bundler; wrote the package management tool for Rust called Cargo.

* * *

For a real getting started in Rust video watch this presentation.

**NYLUG Presents: Steve Klabnik on Why Rust? (July 15, 2015)**

[![](https://metaruby.com/uploads/default/original/2X/d/dae8e6b93398c85200b5ca3aeb9662f20c5a16ad.jpeg "NYLUG Presents: Steve Klabnik on Why Rust? (July 15, 2015)") ](https://www.youtube.com/watch?v=_-fweBvtifA)

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 19, 2015, 5:35pm UTC](https://metaruby.com/t/getting-started-in-rust/375/6 "2015-10-19T17:35:53Z")

</div>

For Rust TDD it’s as easy as follows.

```
cargo new example
cd example

```

No modification to the **.toml** file. And the simple **src/lib.rs**

```auto
pub fn do_the_thing() -> bool {
  true 
}

```

And a simple **tests/lib.rs**

```auto
extern crate example; 

#[test]
fn it_works(){
  assert!(example::do_the_thing())
}

```

And this works! You can further test by proving a failure with **assert!(false)**

**Tip!** _Any line that’s not the last line within a lexical scope needs a semicolon at the end of the chain of command(s). The last line does not get one. (in the examples above everything is the last line within their lexical scopes)_

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 20, 2015, 5:31am UTC](https://metaruby.com/t/getting-started-in-rust/375/7 "2015-10-20T05:31:28Z")

</div>

A great site to learn Rust from is [rustbyexample.com](http://rustbyexample.com)

I’ve written a complete and functional combinatorial count rust example here: [GitHub - danielpclark/combinatorics](https://github.com/danielpclark/combinatorics)

I will write my experience in the process and what I’ve learned from it here later. I’m too tired to continue today. Zzzz…

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 20, 2015, 7:22pm UTC](https://metaruby.com/t/getting-started-in-rust/375/8 "2015-10-20T19:22:08Z")

</div>

Okay, here’s a few things I learned in the process.

1. **Closures** (think of lambdas/procs) are written as follows:

```auto
my_closure = |a: i32, b: i32| -> i32 { a + b }

```

Here **a** and **b** are paremeters that are set to type **32bit integer**. The arrow **→** indicates the type to be output which is also a **32bit integer**. And in between the mustaches is the block of code to execute and return **{ a + b }**.

Rust doesn’t always require the types to be explicitly set as it can infer what they’ll be. But it makes it easier for you to think and debug it when you know what it should be by specifying it yourself.

I wasn’t able to figure out how to inline test a closure as it complains about not being able to pull it in. So in my code I rewrote my closures as normal methods I could test.

1. **Ranges** must be specified from least to greatest (and not the other way around). You can use the **collect** method to return the range as an Array. To tell collect what specific type of collection to return you do it after a double colon and before parenthesis.

```auto
(1..10).collect::<Vec<i32>>()

```

Using Vecors for Arrays allow you to add and remove from it as it is not a fixed size. When you take a slice of the array from the Vector you get a fixed size array. For example slicing from the Array above will return an Array of type **&[i32]** … Vectors and fixed arrays have different methods on them and you can’t pass in a fixed Array where a Vector is expected. On the **&[i32]** type you should call **.to\_vec()** to return a vector.

_In case you’re wondering about the part inside Vec[] and &[] is the type of unit the container keeps inside._

Example (double slashes are for comments)

```auto
let x = (1..10).collect::<Vec<i32>>();
let slice = x[2..6].to_vec()

```

Notice I didn’t specify the type on the let side… that’s optional. IF you wanted to specify the type yourself it would look like.

```auto
let x: Vec<i32> = (1..10).collect::<Vec<i32>>();
let slice: Vec<i32> = x[2..6].to_vec()

```

_Note: the range is non inclusive so if you want to use a range with variables inclusively add a plus one_ **(1…(i+1))**

1. Case statements are done with **match**. In Rust the **match** keyword is best used for **case** statements and **if** - **else** scenarios.

```auto
match testing_this {
  1 => println!("It's 1"),
  2 => println!("It's 2"),
  _ => println!("It's anything other than 1 or 2 and the value is: {}", testing_this),
}

```

Yes the last comma was intentional. It’s in documentation everywhere on demonstrations. I don’t know why they have the comma there. For me it seems to work without it.

The compiler will ensure you have a result for every **match** situation. The underscore thing **\_** is kind of like a “what-ever” object you use to say you don’t care about the value.

1. **Null** / **Nil**. There is no such thing as nil in Rust. If you want to implement a null option you have to create an **Option** object and expect either a **Some** result or a **None** result.

2. When creating a library with **cargo new my\_thing** the resulting code is auto scoped inside the name of the project. In the **Cargo.toml** file where it says **name=“my\_thing”** you’ve already specified something like a module. In your test file you include it with:

```auto
extern crate my_thing;

```

To test the methods in your integration test file you would write:

```auto
#[test]
fn do_the_test {
  assert!(my_thing::some_method) // should return true
}

```

If you want the methods from the “crate” **my\_thing** available in local scope then you would add the following line near the top of your integration test file:

```auto
use my_thing::*;

```

* * *

And that’s that. Everything seems pretty straight forward from there and anything you do wrong it will be sure to tell you about in it’s compiler alert/messages.

---

<div class="post-metadata">

**Author:** ![tak1n](https://metaruby.com/user_avatar/metaruby.com/tak1n/32/862_2.png) [@tak1n](https://metaruby.com/u/tak1n)\
**Post date:** [October 21, 2015, 11:35am UTC](https://metaruby.com/t/getting-started-in-rust/375/9 "2015-10-21T11:35:34Z")

</div>

Also the official doc by steveklabnik should be metioned: [https://doc.rust-lang.org/stable/book](https://doc.rust-lang.org/stable/book) 😄

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 21, 2015, 11:54pm UTC](https://metaruby.com/t/getting-started-in-rust/375/10 "2015-10-21T23:54:01Z")

</div>

I’ve released my first Rust “crate”; the equivalent to a Ruby gem. It’s called [Array Tool](https://github.com/danielpclark/array_tool) v0.1.0. Currently you give it two vectors and it will return two vectors with all common elements removed. I plan on adding many Array utilities over time that are typically available in other higher languages such as Ruby or Python.

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 22, 2015, 9:45pm UTC](https://metaruby.com/t/getting-started-in-rust/375/11 "2015-10-22T21:45:58Z")

</div>

A more advanced instruction for implementing Ruby methods in Rust: [A Rusting Rubyist IV](https://medium.com/@mfpiccolo/a-rusting-rubyist-iv-b1d32802944b#.xf3vnhm6g)

And a cheat sheet for Rubyists to use Rust: [Ruby to Rust Cheat Sheet](https://gist.github.com/carols10cents/f505ed97f495ea37a4b4)

And a general Rust cheat sheet: [Learn X in Y minutes (Where X=rust)](http://learnxinyminutes.com/docs/rust/)

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 22, 2015, 11:52pm UTC](https://metaruby.com/t/getting-started-in-rust/375/12 "2015-10-22T23:52:25Z")

</div>

I’ve integrated Rust methods into Ruby in my [combinatorics](https://github.com/danielpclark/combinatorics) repo and benchmarked it. The Rust methods can easily run about 1000x faster than the Ruby methods.

Here’s the iterations per second results:

```auto
# Permutations
# input language
(12500, 160) in Rust 35.517k (± 0.4%) i/s - 180.359k
(12500, 160) in Ruby 954.776 (± 2.4%) i/s - 4.836k
(1723, 160) in Rust 202.591k (± 1.6%) i/s - 1.021M
(1723, 160) in Ruby 4.610k (± 3.2%) i/s - 23.300k
(500, 160) in Rust 463.864k (± 5.1%) i/s - 2.319M
(500, 160) in Ruby 8.132k (± 6.0%) i/s - 40.800k
(230, 160) in Rust 685.956k (± 1.9%) i/s - 3.455M
(230, 160) in Ruby 10.592k (± 1.7%) i/s - 53.448k

# Combinatorial Count
# input language
(12500, 160) in Rust 939.118k (± 1.8%) i/s - 4.707M
(12500, 160) in Ruby 887.679 (± 1.6%) i/s - 4.488k
(1723, 160) in Rust 948.347k (± 0.8%) i/s - 4.746M
(1723, 160) in Ruby 3.293k (± 2.7%) i/s - 16.650k
(500, 160) in Rust 902.660k (± 5.7%) i/s - 4.526M
(500, 160) in Ruby 4.844k (± 1.7%) i/s - 24.450k
(230, 160) in Rust 912.189k (± 1.7%) i/s - 4.570M        
(230, 160) in Ruby 5.431k (± 1.3%) i/s - 27.350k

```

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 26, 2015, 2:10am UTC](https://metaruby.com/t/getting-started-in-rust/375/13 "2015-10-26T02:10:43Z")

</div>

My favorite reading source for learning Rust… is its source code. **They document like their lives depend on it.** My favorite two files to look at are [Vectors](https://github.com/rust-lang/rust/blob/master/src/libcollections/vec.rs) and [Iterators](https://github.com/rust-lang/rust/blob/master/src/libcore/iter.rs). I can’t help but learn vast amounts. I also peruse the [users forum](https://users.rust-lang.org/) and look at topics I’m not interested in to see peoples thoughts on the language and issues.

Lastly if you choose to maintain a library, this blog post has an amazing tooling setup: [Good Practices for Writing Rust Libraries](https://pascalhertleif.de/artikel/good-practices-for-writing-rust-libraries/)

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [October 29, 2017, 2:51pm UTC](https://metaruby.com/t/getting-started-in-rust/375/14 "2017-10-29T14:51:02Z")

</div>

Might as well tack this one here; I wrote a blog post a while back about [Coming to Rust from Ruby](https://blog.codeship.com/coming-rust-ruby/)
