第一个rust程序

安装rust编程环境

ubuntu下安装

按照官网教程get-started,直接:

curl https://sh.rustup.rs -sSf | sh

但是因为某些原因,无法下载按照脚本

安装反向代理

感谢中科大的开源镜像站为我们提供了方向代理,我们可以按照镜像站的教程rust-static配置反响代理。

export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

当然,我们也可以把这两行代码写道脚本文件~/.bashrc的最后,然后运行

source ~/.bashrc

也可。
也可以编辑/etc/profile,在末尾添加上面的两行,然后

source /etc/profile

配置完成后,重复上一步

curl https://sh.rustup.rs -sSf | sh

配置环境变量

export PATH="$HOME/.cargo/bin:$PATH"

更好的办法是将这一句添加到你的.bashrc文件中

第一个rust程序

文件名: hello_rust.rc
文件代码:

fn main() {
    let s = "Hello, this is my first rust program!";
    println!("{}", s);
}

编译

rustc hello_rust.rc

执行

./hello_rust

输出

Hello, this is my first rust program!

你也可以把编译和执行融合为一体:

rustc hello_rust.rc && ./hello_rust

以上就是我的第一个rust程序,很简单是不是。

如果报错

error: linker `cc` not found

请安装gcc编译器,参考下面的操作

安装gcc/g++编译器

sudo apt-get update & ## sudo apt-get install build-essential

你可能感兴趣的:(rust)