相关疑难解决方法(0)

Rust中的默认整数类型是什么?

当做类似的事情时:

let mut sum = 5 + 10;
Run Code Online (Sandbox Code Playgroud)

的确切类型是sum什么?它是不能溢出的任意大小的类型吗?

types integer rust

11
推荐指数
1
解决办法
307
查看次数

默认浮点类型是什么?

如果变量指定为浮点类型a,则abs可以使用该函数。以下示例正在运行:

fn main() {
    let a = -1.0f64;
    println!("{:?}", a.abs());
}
Run Code Online (Sandbox Code Playgroud)

1它按预期打印。但如果f64省略,则会在编译期间引发错误,如下例所示:

fn main() {
    let a = -1.0;
    println!("{:?}", a.abs());
}
Run Code Online (Sandbox Code Playgroud)

该版本出现以下故障:

   Compiling playground v0.1.0 (file:///C:/git/Rust/playground)
src\main.rs:3:24: 3:29 error: no method named `abs` found for type `_` in the current scope
src\main.rs:3     println!("{:?}", a.abs());
                                     ^~~~~
note: in expansion of format_args!
<std macros>:2:25: 2:56 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: …
Run Code Online (Sandbox Code Playgroud)

floating-point rust

8
推荐指数
1
解决办法
2110
查看次数

为什么我可以在不使用引用的情况下获取它的所有权后访问它?

fn main() {
    let number_list = vec![1, 2, 3, 4, 5];

    let n = number_list[0];
    let r = &number_list[0];

    println!("{} : {} : {} : {}", n, r, number_list[0], &number_list[0]);
}
Run Code Online (Sandbox Code Playgroud)

输出是:

1 : 1 : 1 : 1
Run Code Online (Sandbox Code Playgroud)

另一个问题是,除了参考之外,向量索引与引用和非引用之间的区别是什么?

rust

2
推荐指数
1
解决办法
121
查看次数

标签 统计

rust ×3

floating-point ×1

integer ×1

types ×1