New FAMILUG

The PyMiers

Friday 21 October 2016

Hello, Rust!

Mình thích thì mình học thôi.

Tài liệu: "The Book”,The Rust Programming Language 
Update: tài liệu dễ đọc hơn: http://rust-lang.github.io/book/

Rust thuộc loại ngôn ngữ nào?


Rust là một sản phẩm xuất phát từ Mozilla - tổ chức đứng sau Firefox thần thánh.
Rust is a systems programming language focused on three goals: safety, speed, and concurrency.

Vậy đừng lôi Rust ra làm web rồi chê này kia. Những lĩnh vực mà Rust quảng cáo là phù hợp:
  • embedding in other languages
  • programs with specific space and time requirements
  • and writing low-level code like device drivers and operating systems.
Vậy Rust có thể mang ra code những thứ người ta code bằng C hay C++.

Cài đăt: https://www.rust-lang.org/en-US/downloads.html
Kiểm tra version:
$ rustc --version
rustc 1.12.1 (d4f39402a 2016-10-19)
Những người code Rust được gọi là : Rustaceans

File code Rust có đuôi ".rs". Tên dùng dấu ``_`` để phân cách giữa các từ, như hello_world.rs 
$ cat main.rs
fn main() {
    println!("Hello, FAMILUG!");
}
$ rustc main.rs
$ ./main
Hello, FAMILUG!
Rust là compiled language, tức phải "build" từ file source ".rs" thành file binary, sau đó chạy file binary.
Everything is a tradeoff in language design.

Cargo

Cargo là một phần quan trọng của Rust, quan trọng đến mức khi học lập trình bao nhiêu ngôn ngữ khác bạn sẽ không thấy người ta giới thiệu package manager ngay ở bài Hello, world. Chả ai nói về ``pip`` khi bắt đầu học Python cả.



Cargo làm gì? 3 thứ
- building your code
- downloading the libraries your code depends on
- and building those libraries.
Nó đi kèm với Rust, nên tự có sau khi cài Rust
$ cargo --version
cargo 0.13.0-nightly (109cb7c 2016-08-19)
Cargo cần file code phải nằm trong thư mục ``src``, và một file config tên là Cargo.toml với nội dung:
[package]

name = "hello_world"
version = "0.0.1"
authors = [ "HVN <hvn@familug.org>" ]
Giờ thì build
$ cargo build
   Compiling hello_world v0.0.1 (file:///Users/hvn/me/rusty/hello_world)
    Finished debug [unoptimized + debuginfo] target(s) in 0.28 secs
Rồi chạy
$ ./target/debug/hello_world
Hello, FAMILUG!
Dùng lệnh cargo run để vừa build xong chạy luôn:
$ cargo run
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/hello_world`
Hello, FAMILUG!

Crate /kreɪt/

Eng: A slatted wooden case used for transporting goods.
Vie: sọt để đựng đồ / thùng không đóng kín 

Rust dùng từ  ``crate`` để chỉ library (thư viện) / package
A ‘crate’ is a package of Rust code.
Thử build một software viết bằng Rust lấy từ GitHub về:
ripgrep combines the usability of The Silver Searcher with the raw speed of grep.
$ git clone git@github.com:BurntSushi/ripgrep.git; cd ripgrep
...
$ cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
 Downloading docopt v0.6.86
 Downloading log v0.3.6
 Downloading memmap v0.2.3
 Downloading walkdir v0.1.8
 Downloading term v0.4.4
 Downloading memchr v0.1.11
 Downloading thread_local v0.2.7
 Downloading libc v0.2.17
 Downloading regex v0.1.77
 Downloading rustc-serialize v0.3.19
 Downloading env_logger v0.3.5
 Downloading num_cpus v1.1.0
 Downloading deque v0.3.1
 Downloading lazy_static v0.2.1
 Downloading aho-corasick v0.5.3
 Downloading fnv v1.0.5
 Downloading utf8-ranges v0.1.3
 Downloading regex-syntax v0.3.7
 Downloading thread-id v2.0.0
 Downloading kernel32-sys v0.2.2
 Downloading winapi v0.2.8
 Downloading winapi-build v0.1.1
 Downloading strsim v0.5.1
 Downloading fs2 v0.2.5
 Downloading rand v0.3.14
   Compiling winapi v0.2.8
   Compiling winapi-build v0.1.1
   Compiling strsim v0.5.1
   Compiling log v0.3.6
   Compiling utf8-ranges v0.1.3
   Compiling libc v0.2.17
   Compiling rustc-serialize v0.3.19
   Compiling fnv v1.0.5
   Compiling regex-syntax v0.3.7
   Compiling kernel32-sys v0.2.2
   Compiling lazy_static v0.2.1
   Compiling term v0.4.4
   Compiling walkdir v0.1.8
   Compiling fs2 v0.2.5
   Compiling num_cpus v1.1.0
   Compiling thread-id v2.0.0
   Compiling memchr v0.1.11
   Compiling thread_local v0.2.7
   Compiling rand v0.3.14
   Compiling memmap v0.2.3
   Compiling aho-corasick v0.5.3
   Compiling deque v0.3.1
   Compiling regex v0.1.77
   Compiling env_logger v0.3.5
   Compiling grep v0.1.3 (file:///Users/hvn/me/rusty/ripgrep/grep)
   Compiling globset v0.1.0 (file:///Users/hvn/me/rusty/ripgrep/globset)
   Compiling docopt v0.6.86
   Compiling ripgrep v0.2.3 (file:///Users/hvn/me/rusty/ripgrep)
    Finished debug [unoptimized + debuginfo] target(s) in 31.75 secs
 ./target/debug/rg 127 /etc/hosts
1:127.0.0.1   localhost
Bản build trên là build để debug, chưa tối ưu, build lại với --release:
$ cargo build --release
   Compiling libc v0.2.17
   Compiling strsim v0.5.1
   Compiling rustc-serialize v0.3.19
   Compiling utf8-ranges v0.1.3
   Compiling regex-syntax v0.3.7
   Compiling log v0.3.6
   Compiling lazy_static v0.2.1
   Compiling winapi-build v0.1.1
   Compiling winapi v0.2.8
   Compiling kernel32-sys v0.2.2
   Compiling fnv v1.0.5
   Compiling term v0.4.4
   Compiling walkdir v0.1.8
   Compiling rand v0.3.14
   Compiling fs2 v0.2.5
   Compiling memchr v0.1.11
   Compiling thread-id v2.0.0
   Compiling thread_local v0.2.7
   Compiling memmap v0.2.3
   Compiling aho-corasick v0.5.3
   Compiling num_cpus v1.1.0
   Compiling deque v0.3.1
   Compiling regex v0.1.77
   Compiling grep v0.1.3 (file:///Users/hvn/me/rusty/ripgrep/grep)
   Compiling docopt v0.6.86
   Compiling env_logger v0.3.5
   Compiling globset v0.1.0 (file:///Users/hvn/me/rusty/ripgrep/globset)
   Compiling ripgrep v0.2.3 (file:///Users/hvn/me/rusty/ripgrep)
    Finished release [optimized + debuginfo] target(s) in 77.56 secs
Tạo project mới bằng cargo
 $  cargo new hello_familug --bin
     Created binary (application) `hello_familug` project
Lệnh này tạo ra những thứ sau:
$ find hello_familug/ -depth 1
hello_familug//.git
hello_familug//.gitignore
hello_familug//Cargo.toml
hello_familug//src

Copy lại code helloworld:
$ cp hello_world/src/main.rs hello_familug/src/main.rs
$ cd hello_familug/
$ cargo build --release
   Compiling hello_familug v0.1.0 (file:///Users/hvn/me/rusty/hello_familug)
    Finished release [optimized] target(s) in 0.14 secs
$ ./target/release/hello_familug
Hello, FAMILUG!
Chốt:
Different people learn differently! Choose whatever’s right for you.
Lược dẫn theo: https://doc.rust-lang.org/book/getting-started.html

Hết.
Bài tiếp: Học Rust để tính tóan
HVN at http://familug.org
Bạn muốn tự học Python? bấm ngay vào http://pymi.vn/tutorial/

No comments:

Post a Comment