如何从元组结构中提取值? [英] How do I extract a value from a tuple struct?
问题描述
我有一个结构的对象,它有一个来自外部库的字段,定义为:pub struct SomeId(pub i64);
I have an object of a struct with one field from an external library, which is defined as: pub struct SomeId(pub i64);
使用 println!
打印对象显示了这一点,例如:SomeId(123)
Using println!
to print the object shows this, for example: SomeId(123)
我创建了自己的结构:
#[derive(Debug)]
pub struct Something {
pub id: i64,
}
我正在尝试将外部结构 SomeId
中的值放入我的结构 Something
中的字段 id
中:
And I'm trying to put value from external struct SomeId
to field id
in my struct Something
:
let test = Something { id: ?? };
或从结构SomeId
中提取值:
let test: i64 = ??;
推荐答案
你或许应该试试
let test = Something { id: external_struct.0 };
或者,对于第二个问题:
or, to the second question,:
let test = external_struct.0;
这些结构体,形式为 struct structname(variables...)
被称为元组结构体,其行为与 Rust 中的 tuples
非常相似.
These structs , of the form , struct structname(variables...)
are called tuple structs and acts very similar to tuples
in rust.
这篇关于如何从元组结构中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!