Rust 原生类型

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、标量类型(scalar type)](#一、标量类型(scalar type))
  • [二、 复合类型(compound type)](#二、 复合类型(compound type))
  • 总结

前言

Rust 学习系列 ,rust中的原生类型


一、标量类型(scalar type)

标量类型(scalar type)

  1. 布尔类型(bool):表示真假值。
rust 复制代码
let is_true: bool = true;
let is_false: bool = false;
  1. 字符类型(char):表示单个Unicode字符。
rust 复制代码
let character: char = 'A';
  1. 整数类型(integer):表示整数值,包括有符号和无符号整数。
    有符号整数(signed integers):i8、i16、i32、i64、i128 和 isize(指针宽度)
    无符号整数(unsigned integers): u8、u16、u32、u64、u128 和 usize(指针宽度)
rust 复制代码
let a: i32 = -10; // 有符号整数
let b: u8 = 255; // 无符号整数
  1. 浮点数类型(float):表示浮点数值,包括单精度和双精度浮点数。
rust 复制代码
let c: f32 = 3.14; // 单精度浮点数
let d: f64 = 3.14159; // 双精度浮点数
  1. 字符串类型(str):表示不可变的字符串切片。
rust 复制代码
let message: &str = "Hello, Rust!";

二、 复合类型(compound type)

  1. 数组类型(array):表示固定大小的相同类型元素的集合。
rust 复制代码
let numbers: [i32; 3] = [1, 2, 3];
  1. 元组类型(tuple):表示固定大小的不同类型元素的集合。
rust 复制代码
let person: (String, i32, bool) = ("Alice".to_string(), 25, true);
  1. 指针类型(pointer):表示内存中的地址,有引用和裸指针两种类型。
rust 复制代码
let reference: &i32 = &42; // 引用
let raw_pointer: *const i32 = &42 as *const i32; // 裸指针
  1. Option类型:表示可能存在或不存在的值。
rust 复制代码
let maybe_number: Option<i32> = Some(42);
let no_number: Option<i32> = None;
  1. Result类型:表示操作可能成功或失败的结果。
rust 复制代码
let file_result: Result<File, io::Error> = File::open("example.txt");

总结

以上就是今天要讲的内容,本文简单介绍了rust的原生类型

相关推荐
brzhang12 分钟前
代码即图表:dbdiagram.io让数据库建模变得简单高效
前端·后端·架构
Jamesvalley17 分钟前
【Django】新增字段后兼容旧接口 This field is required
后端·python·django
秋野酱29 分钟前
基于 Spring Boot 的银行柜台管理系统设计与实现(源码+文档+部署讲解)
java·spring boot·后端
JAVA学习通43 分钟前
JAVA多线程(8.0)
java·开发语言
Luck_ff08101 小时前
【Python爬虫详解】第四篇:使用解析库提取网页数据——BeautifuSoup
开发语言·爬虫·python
学渣676561 小时前
什么时候使用Python 虚拟环境(venv)而不用conda
开发语言·python·conda
獨枭1 小时前
Spring Boot 连接 Microsoft SQL Server 实现登录验证
spring boot·后端·microsoft
想睡hhh1 小时前
c++STL——stack、queue、priority_queue的模拟实现
开发语言·c++·stl
shanzhizi1 小时前
springboot入门-controller层
java·spring boot·后端
小鹿鹿啊1 小时前
C语言编程--14.电话号码的字母组合
c语言·开发语言·算法