[RAILS] Experienced Ruby touches Rust and compares it Winter 2020

A little while ago, I had the opportunity to come into contact with new technology at the workplace I was indebted to, so I tried using Rust. Rust has a similar world view to Ruby, and there are many parts that are easy to understand.

This time, I would like to summarize what I did in comparison with the world of Ruby/Rails.

The good thing about Rust

Why did you become interested in Rust in the first place and decided to use it? Rust Official

--Advantages of compiled languages ――I will explain the impressions I have used later. --Good performance ――I will explain the impressions I have used later.

Write Rust

I read Rust's tutorial (The book) and programmed an example called Guessing game.

Impressions

--Ownership gives you more control over the scope lifecycle of variables --Like a guide that can be written naturally when programmed conservatively and prevents careless mistakes --C language/C ++, C #, Ruby-like, grammar is not so difficult if you have a background --When compiled, references and definitions can be tracked properly. Useful for implementation and good for reading ――Since you can't do loose coding with duck typing like Ruby, compilation errors are likely to increase until you get used to it. --The speed of light if you just run what you built --Running is fast, but the binary file size is large ――I personally like it

Term comparison with Ruby

If you understand Ruby, you can get an image by looking at this. I thought that the usability related to Gem was quite similar and easy to use.

Ruby Rust
Gem Crate
Bundler Cargo
Class Struct
Instance method Method
Array Array, Vec, etc.
Hash HashMap etc.
array.each for x in array

Compare API server implementation with Rails

Following a simple example, I implemented the Rest API server in PostgreSQL https://github.com/Samemura/rust-web-api-with-db

Impressions

--There is no small scale --Fast build and execution. I think --It's possible that the library isn't as heavy as Rails. --People who are passionate about architecture seem to be easy to use --Ease of testing cannot be evaluated

Comparison with Rails

This time, I looked at Official and tried using Rocket. As I was told later, it seems that Actix-web is actually popular. After that, Rocket and Rails are compared.

item Rails Rocket
ORM ActiveRecord Diesel

However, it is necessary to use different libraries depending on the DB. Diesel is PostgreSQL and MySQL,SQLite only.
Directory structure Strictly determined freedom

You need to decide and operate the architecture and layer structure yourself
Routing routes.Described in rb with DSL Pass controller-like function when mounting rocket
function attribute(Something like a preprocessor)Define the path with
Controller Implemented in Controller class Implemented with function. No particular restrictions
Request parameters are parsed by defining Struct. It's pretty easy because parse does a good job.
Model Implemented in Model class Implemented with Struct. No particular restrictions
Apart from the ORM, the model used at the time of insert and the one acquired by select can be handled separately.
It feels like there are a lot of Active Models. Similar things are likely to increase, but maintainability seems to be good because the range of influence is clear.
Migration Written in DSL Up/Write SQL separately for Down

I think it's easier to understand if you write it in SQL. Up/Down is also clear.

I made a CLI tool

I heard that it was easy to make, so I came up with something that I could use around me, so I made it. It can be used when randomly deciding the order in the order of presentation. https://github.com/Samemura/kaiten-sushi

Impressions

--It was very easy to publish to crates.io --Click here for the procedure --Installers can easily install using cargo --The method of installing the binaries directly was quite difficult --Since it is listed in the procedure, it seems that you can make it lighter and try it --You need to build each platform with TravisCI so that you can install it with install.sh in a library called Trust. --Build with Travis CI, but deploy task fails --macos is not supported by Trust --Clogged? --You can make mac os binaries by building locally --Trust install.sh fetches the latest version of GitHub Release as an attachment depending on the platform --Name / upload with reference to Travis CI script example ――It was quite difficult, but I managed to do it ――It is certainly easy to install without Cargo ――When I verify it later, it seems that rustc is necessary. --The library interface is suitable for creating CLI, so it was easy to create. --It can be used with npm, and it can be called from Ruby, so it has a future (Routie)

Summary

It's a pretty rough summary, but I think it's good for experienced Ruby users to get a feel for it. I would appreciate it if you could tell me about the installation with Trust if you have certain information.

English information has been translated into Japanese, but the community seems to be mainly in English, so it seemed necessary to search for detailed information.

Recommended Posts

Experienced Ruby touches Rust and compares it Winter 2020
Created a native extension of Ruby with Rust and published it as a gem
Ruby and Gem