在另一个结构中,您如何获得输入以从用户输入中分配结构值?
我正在做一个数字猜测的游戏,猜测最接近的胜利的球员。
struct Player {
name: String,
balance: i32,
betamount: i32,
guess: i32
}
struct Game {
p1: Player,
p2: Player,
random_card: i32
}
impl Game {
pub fn start(&mut self) {
//start function
}
}
我如何在p1
和p2
start> start
函数中分配name
属性?这允许吗?
I am making a number guessing game, where the player who guesses the closest wins.
struct Player {
name: String,
balance: i32,
betamount: i32,
guess: i32
}
struct Game {
p1: Player,
p2: Player,
random_card: i32
}
impl Game {
pub fn start(&mut self) {
//start function
}
}
How I can assign the name
attribute of p1
and p2
inside the start
function? Is this allowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用内部可突变模式。
玩家对象是不可变的,但是如果我们想突变它们的字段,我们可以将它们(字段)包裹在refcell或cell中。
Use the interior mutability pattern.
Player objects are immutable, but if we want to mutate their fields, we can wrap them (the fields) in RefCell or Cell.