rust - 为什么我收到警告 - "field is never read"

标签 rust warnings

我今天开始学习 Rust,但我不明白为什么会收到以下警告:

warning: field is never read: `x`
  --> main.rs:62:9
   |
62 |         x: f64, //Float com 64-bits
   |         ^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: field is never read: `y`
  --> main.rs:63:9
   |
63 |         y: f64,
   |         ^^^^^^

这是我的代码:

struct Vec2{
    x: f64, //Float com 64-bits
    y: f64,
}

let v1 = Vec2{x: 1.0, y: 3.0};

let v2 = Vec2{
    x: 14.0,
    ..v1
};

let _v3 = Vec2{ ..v2};

let v4 = Vec2{ x: 3.0, y: 6.0};
let Vec2{x: _, y: _} = v4;

据我所知,我正在使用 x 和 y,但同时,我不明白为什么我不断收到那些死代码警告。有人可以帮忙吗?

最佳答案

您没有主动从 xy 读取内容。

分别是..v1..v2 部分可以很容易地在没有 y 的情况下存在,因此 rust 在那里看不到“真正的读取”。

关于rust - 为什么我收到警告 - "field is never read",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61413540/

相关文章:

rust - 在 Rust 中与闭包和生命周期作斗争

rust - 在 vscode-lldb 中调试 chrono::DateTime 时如何查看用户友好的格式?

php - 搜索分页给我警告 : Division by zero

linux - 计算常量时出现范围检查错误

c - C 上的单链表 - 警告 C4047

xcode - 在 GCC 中抑制 "does not implement protocol"警告

reference - 当我使用引用而不是拥有的值调用 std::mem::drop 时会发生什么?

rust - Rust 中的共享循环引用

rust - 我是否错误地实现了 IntoIterator 以引用 LazyList 实现,或者这是一个 Rust 错误?

realpath 函数的转换问题(C 编程)