Menu
CoddyTech

Composition and Ownership

Lesson 34 of 48 in Coddy's Rust for Dart Developers course.

Dart composition stores references to nested objects, so passing a Texture into Tile leaves the original reference usable. Rust can store the owned Texture directly as a field, transferring its non-Copy contents into the Tile. The examples read the same linen label, but the Rust original texture binding is no longer available after construction. Moving the outer tile also moves ownership of its contents. Borrowed fields are possible when another owner must retain the data, but then lifetime relationships become part of the type. Choose owned composition when the composite should be self-contained. Delegating through a contained value does not require class inheritance.

Complete standalone Dart program:

class Texture {
  final String label;
  Texture(this.label);
}
class Tile {
  final Texture texture;
  Tile(this.texture);
}
void main() {
  final texture = Texture('linen');
  final tile = Tile(texture);
  print(tile.texture.label);
}

Dart output:

linen

Complete standalone Rust program:

struct Texture { label: String }
struct Tile { texture: Texture }
fn main() {
    let texture = Texture { label: "linen".to_string() };
    let tile = Tile { texture: texture };
    println!("{}", tile.texture.label);
}

Rust output:

linen
challenge icon

Challenge

Easy

Implement stop::Stop with public name: String and minutes: i32 fields. journey::Journey::new() -> Journey creates an empty owned collection. add(&mut self, stop: Stop) takes ownership and appends in arrival order. total(&self) -> i32 returns the sum of minutes without removing stops. finish(self) -> Vec<Stop> consumes the journey and returns every owned stop in order. solve reads complete name minutes pairs. Names have no whitespace; minutes are between 0 and 1000; there are at most 100 stops. Return total:n on the first line and the comma-separated stop names on the second, using empty for an empty journey.

Implement the required exported APIs and return the result from exercise::solve(input: &str) -> String without a final newline. Do not print in editable code. The locked driver checks required APIs independently, reads all stdin, and prints the returned text with one final newline.

Your code goes in exercise.rs, stop.rs, journey.rs. main.rs holds the supplied input/output code and cannot be edited.

Try it yourself

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod stop;
mod journey;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // BEGIN PUBLIC API PROBES
let mut j = journey::Journey::new(); assert_eq!(j.total(), 0);
j.add(stop::Stop { name: "ridge".to_string(), minutes: 23 });
j.add(stop::Stop { name: "ridge".to_string(), minutes: 0 });
j.add(stop::Stop { name: "lake".to_string(), minutes: 8 });
assert_eq!(j.total(), 31); assert_eq!(j.total(), 31);
let out = j.finish(); assert_eq!(out.len(), 3);
assert_eq!((&out[0].name, out[0].minutes), (&"ridge".to_string(), 23));
assert_eq!((&out[1].name, out[1].minutes), (&"ridge".to_string(), 0));
assert_eq!((&out[2].name, out[2].minutes), (&"lake".to_string(), 8));
assert!(journey::Journey::new().finish().is_empty());
    // END PUBLIC API PROBES
    println!("{}", exercise::solve(&input));
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Rust for Dart Developers

Practice on your own: Online Rust compiler