是否有一个功能可以将默认名称放在元组中以获得更好的代码完成和建议?

问题描述 投票:0回答:1

是否有一个功能可以将默认名称放在元组中以获得更好的代码完成和建议?这些名字不是强制性的,只是一个提示。

就像是:

struct Rect(width: i32, height: i32);

let r: Rect = (1, 2);
let (a, b) = r; // names while destructuring can be anything
struct rust tuples
1个回答
3
投票

定义结构有三种方法(The book Chapter 5)。

  1. 声明一个空(零大小)结构 struct Foo;
  2. 声明一个元组结构 struct Bar(i32, u32, String);
  3. 声明具有命名字段的结构 struct Baz { first: i32, second: u32, third: String, }

没有其他办法。

解构适用于所有三种变体。

let a = Foo;
let Bar(f, s, t) = Bar(3, 5, String::from("Hallo"));
let Baz { first, second, third } = Baz { first: 3, second: 5, third: String::from("Hello") };

(Qazxswpoi)

© www.soinside.com 2019 - 2024. All rights reserved.