rustmozilla基金委搞出来的一个新语言.

效率上说和c++差不多,作为一个函数式程序,看起来也挺美观.官方的tutorial也做得很不错.目前为止,rust刷版本号已经刷到了1.1了,个人觉得可以试着做点东西了. 唯一的问题是:貌似还没加入我所用的几个CentOS repo.为了以后的管理方便,我还是用某个普通用户来安装个试试吧!

首先,我们需要去下个安装脚本. 安装脚本可以直接curl下来:

$ wget https://static.rust-lang.org/rustup.sh

也可以clone下那个repo

$ git clone --recurse-submodules https://github.com/rust-lang/rustup.git

然后执行安装脚本. 假定我们打算安装在 ~/.local目录下.

$ ./rustup.sh --prefix=$HOME/.local --disable-sudo

Welcome to Rust.

blahblah...

Continue? (y/N) y

rustup: gpg available. signatures will be verified
blahblah...
install: installing component 'rust-docs'
install: WARNING: failed to run ldconfig. this may happen when not installing as root. run with --verbose to see the error

    Rust is ready to roll.

我们使用了两个参数:

  • --prefix=$HOME/.local 设定了安装目录
  • --disable-sudo 保证脚本不要求sudo

而rust到底安装了哪些内容呢? 如下:

$ tree -L 3
.
├── bin
│   ├── cargo
│   ├── rustc
│   ├── rustdoc
│   └── rust-gdb
├── etc
│   └── bash_completion.d
│       └── cargo
├── lib
│   ├── libarena-7d23ff90.so
│   ├── libflate-7d23ff90.so
│   ├── libfmt_macros-7d23ff90.so
│   ├── libgetopts-7d23ff90.so
│   ├── libgraphviz-7d23ff90.so
│   ├── liblog-7d23ff90.so
│   ├── librbml-7d23ff90.so
│   ├── librustc-7d23ff90.so
│   ├── librustc_back-7d23ff90.so
│   ├── librustc_borrowck-7d23ff90.so
│   ├── librustc_data_structures-7d23ff90.so
│   ├── librustc_driver-7d23ff90.so
│   ├── librustc_lint-7d23ff90.so
│   ├── librustc_llvm-7d23ff90.so
│   ├── librustc_privacy-7d23ff90.so
│   ├── librustc_resolve-7d23ff90.so
│   ├── librustc_trans-7d23ff90.so
│   ├── librustc_typeck-7d23ff90.so
│   ├── librustdoc-7d23ff90.so
│   ├── libserialize-7d23ff90.so
│   ├── libstd-7d23ff90.so
│   ├── libsyntax-7d23ff90.so
│   ├── libterm-7d23ff90.so
│   ├── libtest-7d23ff90.so
│   └── rustlib
│       ├── components
│       ├── etc
│       ├── install.log
│       ├── manifest-cargo
│       ├── manifest-rustc
│       ├── manifest-rust-docs
│       ├── rust-installer-version
│       ├── uninstall.sh
│       └── x86_64-unknown-linux-gnu
└── share
    ├── doc
    │   ├── cargo
    │   └── rust
    ├── man
    │   └── man1
    └── zsh
        └── site-functions

15 directories, 36 files

新增了四个命令: cargo, rustc, rustdoc, rust-gdb. bash_completion添加了cargo相关的脚本,还有一些doc和library.

但是,现在事情还没完成,我们还有一些后续工作.

首先,我们应该把rust的几个bin文件所在路径追加到PATH环境变量.

$ PATH=$PATH:$HOME/.local/bin

这样我们可以直接使用rustc等命令,而不需要写全路径了.

我们可以注意到刚才安装的时候,最后有一个WARNING,它说的是ldconfig失败,动态链接库路径没有配置好. 这时候我们若执行rust的程序,效果如下:

$ rustc
rustc: error while loading shared libraries: librustc_driver-7d23ff90.so: cannot open shared object file: No such file or directory

意思是找不到动态链接库.动态链接库分明在~/.local/lib下啊.我们应该将这个路径加入ld的搜寻路径中.这其实是老问题了,而若我们不想使用sudo的话,最好的办法自然是修改LD_LIBRARY_PATH变量了. 如下:

$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/.local/lib

cargo是rust的官方部署包,它提供了一些bash_complete,可以让我们更愉快地写cargo命令.

$ source  ~/.local/etc/bash_completion.d/cargo

这样我们写下cargo后,使劲按tab,效果如下

$ cargo
bench              locate-project     run
build              login              rustc
clean              new                search
doc                owner              test
fetch              package            update
generate-lockfile  pkgid              verify-project
git-checkout       publish            version
help               read-manifest      yank

为了能每次都自动apply这些环境变量,我们可以在~/.bash_profile配置文件中添加内容如下:

export PATH=$PATH:$HOME/.local/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/.local/lib
if [ -d ~/.local/etc/bash_completion.d ]; then
        . ~/.local/etc/bash_completion.d/*
fi

然后使用命令 source ~/.bash_profile 即可在本session apply这些内容.新开的terminal session也会自动apply这些配置.

以上配置好后,我们可以开始来个试试.

$ cargo new hello_world --bin
$ cd hello_world/
$ cargo run
   Compiling hello_world v0.1.0 (file:///home/yu/tmp/rust/hello_world)
     Running `target/debug/hello_world`
Hello, world!

以后我们打算卸载的话,执行~/.local/lib/rustlib/uninstall.sh脚本,rust会被干净地清理掉, 你也可以再次执行那个脚本, 加上参数"--uninstall", 也可以干净得删除.

附,好多环境变量可以让系统更加符合我们的个人口味.比如

export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/include:$HOME/.local/include
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/include:$HOME/.local/include
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib:$HOME/.local/lib

如上三个环境变量可以让我们写C/C++的时候方便地,从系统级别增加头文件和lib的搜寻路径.

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

1 Comment

Bary · July 3, 2015 at 21:23

Google Chrome 44.0.2383.0 Google Chrome 44.0.2383.0 Windows 7 x64 Edition Windows 7 x64 Edition

语言越来越多,只爱 python。

Leave a Reply to Bary Cancel reply

Your email address will not be published. Required fields are marked *