October 2018
Beginner
180 pages
4h 48m
English
Getting back to our example, we'll create our array of driving directions, and we'll add a something else that isn't a driving direction, just to prove that we can.
Here, we have an array of Any trait references containing driving directions and one other thing:
use std::any::Any;//...pub struct DoesHaveAnyTrait { pub name: String, pub count: i32,}//...let okay = String::from("okay");let directions: [&dyn Any; 7] = [ &Forward{ blocks: 5 }, &Turn{ slight: true, right: false }, &Forward{ blocks: 1 }, &Turn{ slight: false, right: true }, &Forward{ blocks: 2 }, &Stop{}, &DoesHaveAnyTrait{ name: okay, count: 16},];
So far, it looks rather similar to our trait object example, which ...
Read now
Unlock full access