Rust to assembly: Arrays, Tuples, Box, and Option handling

Let's look at the following Rust code to understand how arrays, tuples, Option enums, and Box smart pointer allocations are managed at the assembly level.

The generated code is explained via a flowchart and annotated assembly, making it easy to follow the assembly code without a deep understanding of x86-64 assembly.

:link: Rust to assembly: Arrays, Tuples, Box, and Option handling

pub struct Coordinate(f64, f64);

pub struct Line(Coordinate, Coordinate);

pub fn make_quad_coordinates(maybe_coordinate: Option<Coordinate>)
       -> Option<Box<[Coordinate; 4]>> {
    let Coordinate(x, y) = maybe_coordinate?;
    Some(Box::new([
        Coordinate(x, y),
        Coordinate(-x, -y),
        Coordinate(-x, y),
        Coordinate(x, -y),
    ]))
}

pub fn cross_lines_from_quad_coordinates(
    maybe_coordinate: Option<Coordinate>,) 
     -> Option<(Line, Line)> {
    let [a, b, c, d] = *make_quad_coordinates(maybe_coordinate)?;
    Some((Line(a, b), Line(c, d)))
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.