Rust語言之HelloWorld,rusthelloworld
Rust語言之HelloWorld
參考文檔:
http://doc.crates.io/guide.html
1 什麼是Cargo
相當於maven/ant之於java, automake之於c, Cargo是rust的專案管理工具。用 Cargo 做4件事情:
1) 組態管理
2) 下載項目依賴
3) 調用編譯器rustc編譯,發布程式
4) 總之開發rust的一攬子解決方案
當你安裝了rust, cargo就隨之安裝了。
2 建立第一個rust程式HelloWorld
$ cargo new hello_world --bin
查看一下目錄結構:
$ cd hello_world$ tree ..├── Cargo.toml└── src └── main.rs1 directory, 2 files
編譯一下,並運行之:
$ cargo build$ ./target/debug/hello_worldHello, world!
或者:
$ cargo run
Running `target/debug/hello_world`
Hello, world!
編譯release版本:
$ cargo build --release
產生的程式:
target/release/hello_world
3 發布到伺服器
我在開發機器上(Ubuntu14.04)上編譯的,現在發布到RHEL6伺服器,然後運行:
[root@vm-repo ~]# ./hello_world
./hello_world: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by ./hello_world)
./hello_world: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by ./hello_world)
很顯然,我的RHEL6的GLIBC太舊了。查看一下:
[root@vm-repo ~]# strings /lib64/libc.so.6 |grep GLIBC_
...
GLIBC_2.4
GLIBC_2.5
GLIBC_2.6
GLIBC_2.7
GLIBC_2.8
GLIBC_2.9
GLIBC_2.10
GLIBC_2.11
GLIBC_2.12
GLIBC_PRIVATE
4 升級伺服器GLIBC(失敗)1) 下載glibc (http://ftp.gnu.org/gnu/libc/)
wget -c http://ftp.gnu.org/gnu/libc/glibc-2.22.tar.gz
2) 解壓後安裝
# tar zxf glibc-2.22.tar.gz# cd glibc-2.22# mkdir build# cd build# ../configure# make -j4# make install
...
checking for gawk... gawk
checking version of gawk... 3.1.7, ok
checking if gcc is sufficient to build libc... no
checking for nm... nm
configure: error:
*** These critical programs are missing or too old: as ld compiler
*** Check the INSTALL file for required versions.
看來RHEL6的gcc版本太低。需要升級gcc。太費勁,放棄。
5 直接在RHEL6上裝rust (成功)
# curl -f -L https://static.rust-lang.org/rustup.sh -O# sh rustup.sh
[root@vm-repo experiment]# rustc --version
rustc 1.2.0 (082e47636 2015-08-03)
然後重新建立hello_world:
# cargo new hello_world --bin# cd hello_world# cargo build --release
產生的 target/release/hello_world 可以運行!
================================================
更多內容請詳細閱讀
http://doc.crates.io/guide.html
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。