首页 > 其他分享 >Rust的变量类型__Data type

Rust的变量类型__Data type

时间:2024-04-08 16:59:44浏览次数:15  
标签:__ type number scalar error bit Data Rust

Every value in Rust is of a certain data type, which tells Rust what kind of data is being specified so it knows how to work with that data. We’ll look at two data type subsets: scalar and compound.在Rust中每一个值都有确定的变量类型,以告知Rust使用的数据是哪一种指定的类型,Rust从而得知如何使用该数据,我们将看到两种数据类型子集:scalar和compound。

请牢记Rust是一门静态语言,这意味着在编译时就必须确定数据的类型。编译器通常可以根据数据的值或我们如何使用数据来推断其类型。比如:

// 关键字 变量名: 类型注释 = 变量值
let guess: u32 = "42".parse().expect("Not a number!");

if we don't add the : u32 type annotation shown in the preceding code, Rust will display the following error, which means the complier needs more imformation from us to know which type we want to use.

$ cargo build
   Compiling no_type_annotations v0.1.0 (file:///projects/no_type_annotations)
error[E0282]: type annotations needed
 --> src/main.rs:2:9
  |
2 |     let guess = "42".parse().expect("Not a number!");
  |         ^^^^^
  |
help: consider giving `guess` an explicit type
  |
2 |     let guess: _ = "42".parse().expect("Not a number!");
  |              +++

For more information about this error, try `rustc --explain E0282`.
error: could not compile `no_type_annotations` due to previous error

Scalar Types(标量类型)

scalar type represents a single value. Rust has four primary scalar types: integers, floating-point numbers, Booleans, and characters. You may recognize these from other programming languages. Let’s jump into how they work in Rust.

Integer types
| Length | Signed | Unsigned |
|--------|--------|----------|
|  8-bit |   i8   |    u8    |
| 16-bit |  i16   |   u16    |
| 32-bit |  i32   |   u32    |
| 64-bit |  i64   |   u64    |
|128-bit | i128   |  u128    |
|  arch  | isize  |  usize   |

Each variant can be either signed or unsigned and has an explicit size. Signed and unsigned refer to whether it’s possible for the number to be negative—in other words, whether the number needs to have a sign with it (signed) or whether it will only ever be positive and can therefore be represented without a sign (unsigned).每一种可以是有符号(具有正负标识)的或无符号的(不具有正负标识),并且具有明确的大小。有符号和无符号是指数字是否可能为负数,也就是说数字需要有符号 || 它只会是正数(因此可以无符号来表示)

 

 

标签:__,type,number,scalar,error,bit,Data,Rust
From: https://www.cnblogs.com/ashet/p/18121681

相关文章

  • Linux基本命令入门详解
    Linux基本命令是Linux系统操作的基础,掌握这些命令对于初学者来说至关重要。下面将详细介绍一些常用的Linux基本命令,并附上实际例子。一、目录操作命令pwd:显示当前所在的目录路径。例子:在终端中输入pwd,将显示当前用户所在的目录路径,如/home/user。cd:切换目录。例子:输......
  • Linux命令之lldptool命令
    LLDP是一个数据链路层发现协议,LLDP协议使得接入网络的一台设备可以将其主要的能力,管理地址,设备标识,接口标识等信息发送给接入同一个局域网络的其它设备。lldptool工具采用的是LLDP协议,一般我们使用lldptool是为了得到设备的物理拓扑结构以及管理配置信息,比如说,和eth1网口相连的网......
  • 苍穹外卖10(Spring Task定时任务,WebSocket双向通信,订单状态定时处理,来电提醒,客户催单)
    目录一、SpringTask1.介绍2.入门1使用步骤2使用示例3.详解1@Scheduled注解2cron表达式1cron表达式6个域2各个域的取值说明4.小结二、订单状态定时处理1.需求分析1问题分析2功能需求2.代码开发1修改引导类加@EnableScheduling2创建OrderTask......
  • Linux之网络排错
    Linux网卡收包流程如下网卡收到数据包将数据包从网卡硬件缓存移动到服务器内存中(DMA方式,不经过CPU)通过硬中断通知CPU处理CPU通过软中断通知内核处理经过TCP/IP协议栈处理应用程序通过read()从socketbuffer读取数据网卡丢包我们先看下ifconfig的输出:#ifconfigeth......
  • 我要点名一款十字线上 PVP 游戏 - 1951
    \(1900-12=1888\)。怎么rating还是这么好笑。感觉每回打cf都要破防是怎么回事?被诈骗不还是因为菜?交\(12\)发不知道自己是怎么想的。然后E也不难,但是太晚了打不动了。下次交代码之前能不能拜托先把hack测一下?占了将近一半的RE哪个不是因为没开longlong?A01字符串......
  • 干货教程【软件篇】| PDF转换word工具永久免费使用
    给大家分享一个好用的PDF转换word的工具,完全免费、离线使用、且保存下来永久好用的工具。ps:本文只做好用的工具分享,不涉及任何工具的开发,感谢工具的开发者!关注文章下方公众号回复关键词【ptow】即可免费获取本工具。大家下载好之后就会进入安装界面,安装过程十分顺畅这里......
  • Vue.nextTick() 使用场景及实现原理
    Vue.nextTick()基本使用作用:等待下一次DOM更新刷新的工具方法。为什么需要用到Vue.nextTick()?当你在Vue中更改响应式状态时,最终的DOM更新并不是同步生效的,而是由Vue将它们缓存在一个队列中,直到下一个“tick”才一起执行。这样是为了确保每个组件无论发生多少......
  • MySql添加用户
    添加MySQL用户通常涉及创建一个新用户并为其分配相应的权限。以下是在MySQL中添加用户的一般步骤:连接到MySQL数据库服务器:mysql-uroot-p创建一个新用户并分配密码:CREATEUSER'new_user'@'localhost'IDENTIFIEDBY'password';请将'new_user'替换为新用户......
  • 汇编语言程序设计实验五 条件转移指令
    实验目的和要求(1)     编写实验任务要求的两个程序。(2)     写出调试以上程序,即修改程序参数,检查结果的操作方法。(3)     熟悉源程序汇编、连接命令的使用方法即要回答的内容。实验环境DOSBOX实验内容与过程验证以下程序16进制数化ASCII码的一般......
  • Vue3 · 小白学习全局 API:常规
    全局API:常规本次笔记versionnextTick()defineComponent()defineAsyncComponent()defineCustomElement()1.version暴露当前所使用的Vue版本。类型string示例import{version}from'vue'console.log(version)2.nextTick()等待下一次DOM更新刷新的工具......