首页 > 编程语言 >Rust编程基础

Rust编程基础

时间:2022-10-17 18:02:38浏览次数:68  
标签:cargo rs default 编程 基础 Rust hello rust

Rust是一门系统编程语言,专注于安全,尤其是并发安全,支持函数式和命令式以及泛型等编程范式的多范式语言。Rust在语法上和C++类似,但是设计者想要在保证性能的同时提供更好的内存安全。

1.下载安装

官网:https://www.rust-lang.org

Windows版本下载(先安装Visual Studio,社区版免费):

https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe

Linux版本下载(Ubuntu为例):

$ sudo apt update
$ sudo apt install build-essential curl vim
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

安装Windows、Linux差不多,安装出现过程如下选项:

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

我们选择1,默认安装,之后会进行在线安装,离线安装等访问参考官网Get-Started

在Linux版本,安装成功后需要运行一下命令(Windows不需要此步骤):

Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source "$HOME/.cargo/env"

即,命令行运行:

source "$HOME/.cargo/env"

2.基础示例

方法1:

$ cargo new hello-rust
$ cargo run
    Compiling hello-rust v0.1.0 (/Users/ag_dubs/rust/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 1.34s
     Running `target/debug/hello-rust`
Hello, world!

方法2(以Linux为例,Windows类似):

$ vim hello.rs

在hello.rs文件中编写并保存如下内容:

1  fn main()
2 {
3     println!("Hello World.");
4 }

编译:

$ rustc hello.rs
$ ./hello
Hello World.

3. 基础语法

4. 数据类型

5. 注释

6. 函数

7. 条件语句

8. 循环

9. 所有权

10. 切片

11. 结构体

12. 枚举类

13. 组织管理

14. 错误处理

15. 泛型

16. 生命周期

17. 文件IO

18. 集合与字符串

19. 面向对象

标签:cargo,rs,default,编程,基础,Rust,hello,rust
From: https://www.cnblogs.com/phoebus-ma/p/16800074.html

相关文章