Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #[derive(Debug)]
  2. pub struct Context {
  3. pub name: String
  4. }
  5.  
  6.  
  7. impl Context {
  8. pub fn new(string: &str) -> Context {
  9. Context {
  10. name: String::from(string)
  11. }
  12. }
  13. }
  14.  
  15. #[derive(Debug)]
  16. pub struct Data<'a> {
  17. pub data: Vec<f32>,
  18. pub context: &'a mut Context
  19. }
  20.  
  21. impl<'a> Data<'a> {
  22. pub fn new(context: &'a mut Context) -> Data<'a> {
  23. Data {
  24. data: Vec::new(),
  25. context
  26. }
  27. }
  28.  
  29. pub fn from_slice(data: &[f32], context: &'a mut Context) -> Data<'a> {
  30. Data {
  31. data: data.to_vec(),
  32. context
  33. }
  34. }
  35.  
  36. pub fn clear(&mut self) {
  37. self.data = Vec::new()
  38. }
  39. }
  40.  
  41. fn push_more(d: &mut Data) {
  42. d.data.push(3.0);
  43. }
  44.  
  45. fn main() {
  46. let mut context = Context::new("my context");
  47. let mut d = Data::from_slice(&[1.0, 2.0, 3.0], &mut context);
  48. d.clear();
  49. d.data.push(1.0);
  50. d.data.push(2.0);
  51. d.context.name.push_str("!");
  52. push_more(&mut d);
  53.  
  54. println!("{:?}", d);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement