Rust語言之HelloWorld,rusthelloworld

來源:互聯網
上載者:User

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


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.