Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Last updated: Friday 13 November 2026 @ 17:29:40
BeagleBone Black · Rust

Your First Rust Programme

Write a small text-mocking programme directly on the BeagleBone Black Revision D. Start with a single source file, compile it using rustc, and progressively introduce functions, structs, methods and traits.

rustcFunctionsStructsTraitsImplementations
Practical Checklist
Chapter checklist 0 / 10 completed

1. Create and compile a Rust programme

  1. Connect to your BeagleBone Black using the SSH procedure from Getting Started. Confirm the compiler is available.

    rustc --version
    uname -m

    The architecture should report armv7l. Unlike a hosted browser playground, rustc here produces a native executable for the board.

  2. Create a new directory and open a source file.

    • rust/
      • first_program/
        • main.rs
    mkdir -p ~/rust/first_program
    cd ~/rust/first_program
    nano main.rs

    A .rs file contains Rust source code. Save and exit nano after entering the code below.

  3. Write the smallest useful programme.

    fn main() {
        println!("Hello from the BeagleBone Black!");
    }

    fn declares a function, main is the entry point, and println! is a macro (note the !).

  4. Compile and execute it.

    rustc main.rs
    ./main

    Expected output:

    Hello from the BeagleBone Black!

    rustc main.rs creates an executable named main by default. ./main runs it from the current directory.

2. Variables and text

  1. Replace the contents of main.rs with:

    fn main() {
        let message = "Hello";
        println!("Original: {}", message);
    }

    let introduces a binding. message is immutable by default; the string literal has type &str.

  2. Compile and run again, this time choosing an executable name:

    rustc main.rs -o text_mocker
    ./text_mocker
    Original: Hello
  3. Run the code here and read about the difference between &str and String:

    fn main() {
       let borrowed: &str = "Hello";
       let mut owned = String::from("Goodbye");
    
       owned.push_str(", Rust!");
    
       println!("{}", borrowed);
       println!("{}", owned);
    }

3. Functions and return values

  1. Replace main.rs with the following code. It makes alternating characters lowercase and uppercase, starting with lowercase.

    fn mock_text(input: &str) -> String {
        let mut result = String::new();
        for (index, character) in input.chars().enumerate() {
            if index % 2 == 0 {
                result.push(character.to_ascii_lowercase());
            } else {
                result.push(character.to_ascii_uppercase());
            }
        }
        result
    }
    
    fn main() {
        println!("{}", mock_text("Hello"));
    }

    Observe that -> String declares a return type and the final result expression has no trailing semicolon. mut permits modification of the local result variable.

  2. Compile and run it.

    rustc main.rs -o text_mocker
    ./text_mocker
    hElLo

4. Structs and inherent implementations

  1. Replace the programme with a TextMocker struct and an inherent implementation (impl TextMocker).

    struct TextMocker {
        text: String,
    }
    
    impl TextMocker {
        fn new(text: &str) -> Self {
            Self { text: String::from(text) }
        }
    
        fn render(&self) -> String {
            let mut result = String::new();
            for (index, character) in self.text.chars().enumerate() {
                if index % 2 == 0 {
                    result.push(character.to_ascii_lowercase());
                } else {
                    result.push(character.to_ascii_uppercase());
                }
            }
            result
        }
    }
    
    fn main() {
        let message = TextMocker::new("Hello");
        println!("{}", message.render());
    }

    Self represents the type being implemented. &self borrows the struct for reading without moving or changing its stored String.

  2. Build and run using the same compiler command.

    rustc main.rs -o text_mocker
    ./text_mocker
    hElLo

5. Traits and trait implementations

A trait describes behaviour that types may implement. It does not contain the text itself; the struct stores data, while the trait specifies an operation.

  1. Replace main.rs with the complete version below. Compare the two different forms of impl.

    struct TextMocker {
        text: String,
    }
    
    impl TextMocker {
        fn new(text: &str) -> Self {
            Self { text: String::from(text) }
        }
    }
    
    trait Transform {
        fn transform(&self) -> String;
    }
    
    impl Transform for TextMocker {
        fn transform(&self) -> String {
            let mut result = String::new();
            for (index, character) in self.text.chars().enumerate() {
                if index % 2 == 0 {
                    result.push(character.to_ascii_lowercase());
                } else {
                    result.push(character.to_ascii_uppercase());
                }
            }
            result
        }
    }
    
    fn main() {
        let message = TextMocker::new("Hello");
        println!("{}", message.transform());
    }
  2. Compile and run.

    rustc main.rs -o text_mocker
    ./text_mocker
    hElLo

6. Read and use compiler errors

  1. In the last main() function, temporarily change message.transform() to message.unknown_method() and compile again.

    rustc main.rs -o text_mocker

    Inspect the diagnostic. Which type is involved? Which method could the compiler not find?

  2. Restore message.transform() and compile successfully before continuing to the next chapter.

7. Independent exercise

Implement Transform for Shout and use self.text.to_ascii_uppercase(). The implementation of the trait method must have the same signature: fn transform(&self) -> String.

The following is an addition to the working programme; insert it before main() and update main() as shown.

struct Shout {
    text: String,
}

impl Transform for Shout {
    fn transform(&self) -> String {
        self.text.to_ascii_uppercase()
    }
}

fn main() {
    let mocker = TextMocker::new("Hello");
    let shout = Shout { text: String::from("Hello") };
    println!("{}", mocker.transform());
    println!("{}", shout.transform());
}

Expected output: hElLo and HELLO on separate lines.