如何在生锈中多次使用变量?
我知道Rust中借贷和拥有的基本原则。 但是,我找不到解决以下情况的方法:
fn create_user(full_name: String) -> User {
User {
field1: full_name,
field2: full_name,
}
}
如果我想在上面的示例中将几个字段引用给给定参数怎么办?使用“*”不能解决问题,也没有编译。
在询问之前,我真的没有找到这个问题的答案。
I know about the basic principles of borrowing and owning in Rust.
However, I can't find a way to solve the following situation:
fn create_user(full_name: String) -> User {
User {
field1: full_name,
field2: full_name,
}
}
What if I would like to reference couple of fields in the example above to the given argument? Using of the "*" doesn't solve the problem and is not compiling.
I really did not find an answer to this question for a while before asking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些选项,包括(可能不仅):
如果
t
是clone
如果
t
不是clone> clone
,您可以利用field2作为返回参考的方法:There are a few options, including (and probably not only):
If
T
isClone
if
T
is notClone
, you could leverage field2 as a method returning a reference: