Skip to content

Potential name typo in borrowed pointer tutorial. #3876

Closed
@nejucomo

Description

@nejucomo

The following snippet confused me a bit. I believe "shape()" should be replaced with "select()" in the explanatory paragraph for the code snippet.

From commit 07edf90 tutorial-borrowed-ptr.md, line 674:

struct Point {x: float, y: float}; // as before

struct Size {w: float, h: float}; // as before

enum Shape {

Circle(Point, float), // origin, radius

Rectangle(Point, Size) // upper-left, dimensions

}

fn compute_area(shape: &Shape) -> float { 0f }

fn select(shape: &Shape, threshold: float,

a: &r/T, b: &r/T) -> &r/T {

if compute_area(shape) > threshold {a} else {b}

}

                                              // -+ r

fn select_based_on_unit_circle( // |-+ B
threshold: float, a: &r/T, b: &r/T) -> &r/T { // | |
// | |
let shape = Circle(Point {x: 0., y: 0.}, 1.); // | |
select(&shape, threshold, a, b) // | |
} // |-+
// -+


In this call to `select()`, the lifetime of the first parameter shape
is B, the function body. Both of the second two parameters `a` and `b`
share the same lifetime, `r`, which is a lifetime parameter of
`select_based_on_unit_circle()`. The caller will infer the
intersection of these two lifetimes as the lifetime of the returned
value, and hence the return value of `shape()` will be assigned a
lifetime of B. This will in turn lead to a compilation error, because
`select_based_on_unit_circle()` is supposed to return a value with the
lifetime `r`.

There is no function called "shape()", but "select()" would be consistent.

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