# Is there way to assign tuple returned by function to tuple in vector

See 'Is there a way to make this work?' in the following code:

``````#[derive(Copy, Clone, Eq, Hash, Debug, PartialEq)]
pub enum ChessPiece {
WhiteKing,
WhiteQueen,
WhiteRook,
WhiteKnight,
WhiteBishop,
WhitePawn,
BlackKing,
BlackQueen,
BlackRook,
BlackKnight,
BlackBishop,
BlackPawn,
NoPiece,
}

pub fn get_clicked_square_id(x: f64, y: f64, scalar: f64) -> (f64, f64) {
let board_width_height = 8f64;
let range = 0f64..(scalar * board_width_height as f64);

if !range.contains(&x) && range.contains(&y) {
panic!("No Square ID")
}
(
(x.div_euclid(scalar) * scalar),
(y.div_euclid(scalar) * scalar),
)
}

pub fn main() {

let mut tuple_vec2: Vec<_> = vec![
(29.0, 29.0, ChessPiece::WhiteRook, "a1"),
(86.0, 0.0, ChessPiece::WhiteRook, "h1"),
];

println!(
"tuple_vec2[1].0 = {:?}, tuple_vec2[1].1 = {:?}",
tuple_vec2[1].0, tuple_vec2[1].1
);

// ============================Is there a way to make this work in one line like this?
(tuple_vec2[1].0, tuple_vec2[1].1) = get_clicked_square_id(100f64, 100f64, 57f64);
// rather than
let (x, y) = get_clicked_square_id(100f64, 100f64, 57f64);
tuple_vec2[1].0 = x;
tuple_vec2[1].1 = y;
// ============================

println!(
"tuple_vec2[1].0 = {:?}, tuple_vec2[1].1 = {:?}",
tuple_vec2[1].0, tuple_vec2[1].1
);
}
``````

No, there’s not a way to do that.

You might want to make a Position struct that has x and y members though which side steps the issue you’re having by having a single returned value from the function. As a benefit it makes the code a bit more clear about its intent.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.