Can I take a pointer to a common trait in Rust?

From the tutorial on borrowed pointers (broken), slightly modified:

struct Point {x: float, y: float} fn compute(p1 : &Point) {} fn main() { let shared_box : @Point = @Point {x: 5.0, y: 1.0}; compute(shared_box); } 

And that's all right, because the shared box is automatically borrowed for the function.

But do the same with the sign:

 struct Point {x: float, y: float} trait TPoint {} impl TPoint for Point {} fn compute(p1 : &TPoint) {} fn main() { let shared_box : @TPoint = @Point {x: 5.0, y: 1.0} as @TPoint; compute(shared_box); // ^~~~~~~ The error is here } 

And it fails (compiler version 0.6) saying:

error: inappropriate types: expected &TPoint , but found @TPoint (attribute store is different: expected, but found @)

Is this a bug in the compiler? Or are borrowed pointers not allowed for features?

If the answer is the last, why?

+6
source share
1 answer

This is a known bug in the current version of Rust:

# 3794: Casting to a tag does not automatically amplify the & T type

Some work has been undertaken to try to solve this problem, but there are some technical details that need to be smoothed out; interested parties can see some of the discussions (from a few months ago) here, at the request of 4178 .

+3
source

Source: https://habr.com/ru/post/944154/


All Articles