Reputation: 556
Here is the struct Area
that I am using for a Snake program:
struct Area {
max_x: usize,
max_y: usize,
arr: Vec<Vec<&'static str>>
}
And here is the function that uses the arr
attribute from the struct Area
:
fn refresh(area: &Area) {
println!("{:?}", area.arr)
}
Since it only needs that one attribute from the struct, I was hoping that I could have the parameter be that one named attribute so that I wouldn't have to write as much. It would look like this (This produces an error):
fn refresh(array: &Area.arr) {
println!("{:?}", array)
}
Is there any workaround to doing something similar to this?
Upvotes: 2
Views: 467
Reputation: 8331
Yes, you can change the signature of your refresh
function like so
fn refresh(area_arr: &Vec<Vec<&'static str>>) {
println!("{:?}", area_arr)
}
and then you can call it like this
refresh(&your_area_instance.arr);
Here is a full code example
struct Area {
max_x: usize,
max_y: usize,
arr: Vec<Vec<&'static str>>
}
fn main() {
let area_instance = Area {
max_x: 20,
max_y: 20,
arr: vec![
vec![&"test1_1", &"test1_2"],
vec![&"test2_1", &"test2_2"],
]
};
refresh(&area_instance.arr);
}
fn refresh(area_arr: &Vec<Vec<&'static str>>) {
println!("{:?}", area_arr)
}
and a Playground link.
Upvotes: 4