extern crate rand;
use std::io;
use std::cmp::Ordering;
use rand::Rng;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1, 101);
loop {
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}
https://insights.stackoverflow.com/survey/2015
https://insights.stackoverflow.com/survey/2016
https://insights.stackoverflow.com/survey/2017
$ curl https://sh.rustup.rs -sSf | sh
$ rustup update
$ rails new --skip-active-record tryhelix
source 'https://rubygems.org'
# ...
gem 'helix-rails', '~> 0.5.0'
Rails.application.routes.draw do
get '/ruby', to: 'static#ruby'
get '/helix', to: 'static#helix'
end
<h1>Ruby</h1>
<h1>Helix</h1>
class StaticController < ApplicationController
def ruby
end
def helix
end
end
module CurrentBenchmark
def print_time_spent
@time = Benchmark.realtime do
yield
end
end
end
include CurrentBenchmark
def ruby
print_time_spent do
end
end
def helix
print_time_spent do
end
end
require 'csv'
...
private
def path
Rails.root.join('tmp', 'some.csv').to_s
end
def ruby
@sum = 0
print_time_spent do
CSV.foreach(path, headers: true) do |row|
@sum += row['id'].to_i
end
end
end
<h1>Ruby</h1>
<p>
Total: <pre><%= @sum %></pre>
Time: <%= @time * 1000 %> ms
</p>
rails generate helix:crate csv_reader
#[macro_use]
extern crate helix;
ruby! {
class CsvReader {
def hello() {
println!("Hello from csv_reader!");
}
}
}
$ cd crates/csv_reader/
$ rake irb
...
>> CsvReader.hello
Hello from csv_reader!
=> nil
>> exit
# encoding: utf-8
Gem::Specification.new do |s|
...
s.add_development_dependency 'rspec', '~> 3.6'
end
require 'csv_reader'
describe 'CsvReader' do
it 'returns integer' do
filename = '/home/karuna/helix/tryhelix/tryhelix/tmp/some.csv'
expect(CsvReader.count_id(filename).class).to eq(Integer)
end
end
rspec
class CsvReader {
def count_id(filename: String) -> i64 {
0
}
}
rspec
rake build
rspec
[dependencies]
...
csv = "0.15.0"
extern crate csv;
...
def count_id(filename: String) -> i64 {
let mut total : i64 = 0;
let mut rdr = csv::Reader::from_file(filename)
.unwrap()
.has_headers(true);
for row in rdr.records() {
let record = row.unwrap();
let new_number : i64 = record[0].trim()
.parse()
.unwrap();
total += new_number;
}
total
}
def helix
print_time_spent do
@sum = CsvReader.count_id(path)
end
end
<h1>Helix</h1>
<p>
Total: <pre><%= @sum %></pre>
Time: <%= @time * 1000 %> ms
</p>
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[test]
fn it_works() {
assert_eq!(4, add_two(2));
}
//! The `adder` crate provides functions that add numbers to other numbers.
//!
//! # Examples
//!
//! ```
//! assert_eq!(4, adder::add_two(2));
//! ```
/// This function adds two to its argument.
///
/// # Examples
///
/// ```
/// use adder::add_two;
///
/// assert_eq!(4, add_two(2));
/// ```
pub fn add_two(a: i32) -> i32 {
a + 2
}