Skip to content

Activity for 1.2.2 Display - Formatting a Complex Struct #1678

Open
@j-medland

Description

@j-medland

I am having some difficulty with achieving the activity in 1.2.2 Display due to the presence of a space between the sign and the imaginary component.

The activity asks to add a fmt::Display implementation for a struct representing a complex number with real and imaginary f64 values with the output:

3.3 + 7.2i

Using "{} {:+}i" would seem like the reasonable approach but this yields 3.3 +7.2i and it is not obvious how I can pad between the sign and imaginary component to get the output specified. As the activity doesn't test complex numbers with a negative imaginary component, some folks might just use "{} + {}i" and move on to the next lesson.

So am I missing something - is there a way to add padding? should I use some conditional statement based on the sign of the imaginary component?

I noticed this change was introduced by PR #1123 where previous to this, there was no space.

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions