rust 程序设计第一节 Linux 环境搭建与小案例


1.安装
  

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

2.更新
    

$ rustup update

3.卸载
    

$ rustup update


    
4.设置环境变量
    

$ export PATH=$PATH:/home/opencl/.cargo/bin


    或将

export PATH=$PATH:/home/opencl/.cargo/bin

添加为 /etc/profile  or  ~/.profile 最后一行,并source 之
    
5.看本地文档
  

$ rustup doc


    
6.示例编译

$ cat hello_world.rs
fn main(){
    println!("Hello world!");
}

$ rustc ./hello_world.rs
$ ls
hello_world  hello_world.rs
$ ./hello_world
Hello world!
$ 

7.测试cargo

$ cargo --version


cargo 1.60.0 (d1fd9fe 2022-03-01)

8.用 cargo 创建 rust 项目

$ cargo new hello_cargo
     Created binary (application) `hello_cargo` package
$ ll
total 12
drwxrwxr-x 3 opencl opencl 4096 5月  18 11:23 ./
drwxrwxr-x 4 opencl opencl 4096 5月  18 11:22 ../
drwxrwxr-x 4 opencl opencl 4096 5月  18 11:23 hello_cargo/
$ ls ./hello_cargo/
Cargo.toml  src
$ ls ./hello_cargo/src/
main.rs
$ cat ./hello_cargo/src/main.rs
fn main() {
    println!("Hello, world!");
}
$


9. 运行cargo项目

$ cd hello_cargo/
$ cargo run
   Compiling hello_cargo v0.1.0 (/home/opencl/ex/rustEx/helloCargo/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target/debug/hello_cargo`
Hello, world!
$

10.编写程序时检查语法

$ cargo check
    Checking hello_cargo v0.1.0 (/home/opencl/ex/rustEx/helloCargo/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.06s

11.编译发布版本

$ cargo build --release
   Compiling hello_cargo v0.1.0 (/home/opencl/ex/rustEx/helloCargo/hello_cargo)
    Finished release [optimized] target(s) in 0.12s
$ ls
Cargo.lock  Cargo.toml  src  target
$ ls target/
CACHEDIR.TAG      debug/            release/          .rustc_info.json
$ ls target/release/
build  deps  examples  hello_cargo  hello_cargo.d  incremental
$
$ cd ./target/release/
$ ./hello_cargo
Hello, world!
$

12.小案例

$ cargo update
    Updating crates.io index
$ cargo run


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值