Relative Content

Tag Archive for rust

Casting a number from one type to another: the trait bound `usize: From` is not satisfied

I have a function that gets the dimensions of an image, and in different scenarios, I would it to be cast from a u32 to another number type suchas usize, i32, etc. However, when I try to do the following in get_dimensions it doesn’t work. Is something like this possible? I looked at How do I convert between numeric types safely and idiomatically? but in that example it uses an explicit type such as i32::from(...) whereas I would hopefully like to use a dynamic type.

How do you access enum values in Rust?

struct Point { x: f64, y: f64, } enum Shape { Circle(Point, f64), Rectangle(Point, Point), } let my_shape = Shape::Circle(Point { x: 0.0, y: 0.0 }, 10.0); I want to print out circle‘s second property, which is 10.0 here. I tried my_shape.last and my_shape.second, but neither worked. What should I do in order to print […]