traits

Iterating over a range of generic type

血红的双手。 提交于 2020-01-13 11:06:41
问题 I have a trait trait B { type Index: Sized + Copy; fn bounds(&self) -> (Self::Index, Self::Index); } I want to get all the Index es within bounds : fn iterate<T: B>(it: &T) { let (low, high) = it.bounds(); for i in low..high {} } This won't work since there's no constraint that the type T can be "ranged" over, and the compiler says as much: error[E0277]: the trait bound `<T as B>::Index: std::iter::Step` is not satisfied --> src/main.rs:8:5 | 8 | for i in low..high {} | ^^^^^^^^^^^^^^^^^^^^^

Why is Fn derived from FnMut (which is derived from FnOnce)?

好久不见. 提交于 2020-01-13 10:34:21
问题 If you look in the official Rust doc, you see that the trait Fn is derived from FnMut , or, to implement Fn , you have to implement FnMut (and after that FnOnce since FnMut also derives from it). Why is that so? I simply can't comprehend that. Is it because you can call every Fn as a FnOnce or FnMut ? 回答1: The best reference for this is the excellent Finding Closure in Rust blog post. I'll quote the salient part: There’s three traits, and so seven non-empty sets of traits that could possibly

Simple organization of Rust traits for “polymorphic” return

試著忘記壹切 提交于 2020-01-11 06:38:07
问题 I have a basic struct called Frame that is useful for a bunch of calculations:. pub struct Frame<T> { grid_val: Vec<T>, grid_space: Vec<[T; 2]>, calculated_result: Option<Vec<T>> } Frame can be used to describe most basic calculations, but sometimes there's more complicated issues that come up and I need to add some more geometric information. So I used composition for each geometry: pub struct Sphere<T> { grid: Frame<T>, radius: T } pub struct Hyperbola<T> { top_grid: Frame<T>, bottom_grid:

Constructors, templates and non-type parameters

半城伤御伤魂 提交于 2020-01-10 17:44:31
问题 I've a class that must depend for some reasons from an int template parameter. For the same reasons, that parameter cannot be part of the parameter list for the class, instead it is part of the parameter list of its constructor (that is, of course, templated). Here the problems arose. Maybe I'm missing something, but I can't see an easy way to provide such a parameter to the constructor, because it cannot be deduced nor explicitly specified. So far, I've found the following alternatives: put

Rust Trait object conversion

人盡茶涼 提交于 2020-01-09 11:40:08
问题 The following code won't compile due to two instances of this error: error[E0277]: the trait bound Self: std::marker::Sized is not satisfied I don't understand why Sized is required in this instance as both &self and &Any are pointers and the operation does not require knowledge of the size of the structure that implements the trait, it only requires knowledge of the pointer itself and the type it is converting from and to, which it will have because &self is generic when implemented inside a

Can't understand Rust module system

孤人 提交于 2020-01-09 08:13:27
问题 I created a simple project for educational purpose, so I have a main function and 3 traits Battery , Display and GSM and implementations for them. I want the the main function to be in file main.rs and the 3 traits in another file called phone.rs: phone.rs mod phone{ pub struct Battery{ model : String, hours_idle : i16, hours_talk : i16 } pub struct Display{ size : i16, number_of_colors : i32 } pub struct GSM{ model : String, manufactor : String, price : f32, owner : String, battery: Battery,

Can't understand Rust module system

旧时模样 提交于 2020-01-09 08:12:31
问题 I created a simple project for educational purpose, so I have a main function and 3 traits Battery , Display and GSM and implementations for them. I want the the main function to be in file main.rs and the 3 traits in another file called phone.rs: phone.rs mod phone{ pub struct Battery{ model : String, hours_idle : i16, hours_talk : i16 } pub struct Display{ size : i16, number_of_colors : i32 } pub struct GSM{ model : String, manufactor : String, price : f32, owner : String, battery: Battery,

Cake pattern with overriding abstract type don't work with Upper Type Bounds

流过昼夜 提交于 2020-01-09 02:29:04
问题 I want to override abstract type in trait with <: and not with = (like answer here Scala Upper Bounds : value is not a member of type parameter). I want to use cake pattern, but this doesn't work, i don't understand why ? trait A { def ping = println("ping") } trait Cake { type T } trait S { this: Cake => type T = A def t: T t.ping } OK, this example run, but in my real use case i want to override type with <: and not with = .It seems impossible to access the t function, why ? trait S { this:

Scala trait implementation

試著忘記壹切 提交于 2020-01-06 06:46:37
问题 I have a case class: case class EvaluateAddress(addressFormat: String, screeningAddressType: String, value: Option[String]) { } This was working fine until I have a new use case where "value" parameter can be a class Object instead of String. My initial implementation to handle this use case: case class EvaluateAddress(addressFormat: String, screeningAddressType: String, addressId: Option[String], addressValue: Option[MailingAddress]) { @JsonProperty("value") def setAddressId(addressId:

Error E0201 when implementing foreign trait for local type with parameter

我们两清 提交于 2020-01-05 04:17:05
问题 I'm trying to add the C type parameter to this code (playground): use std::ops::Index; struct ConnectionHandle(usize); struct Connection<C>(C); impl<C> Index<ConnectionHandle> for Vec<Connection<C>> { type Output = Connection<C>; fn index(&self, ch: ConnectionHandle) -> &Self::Output { &self[ch.0] } } But doing so causes this error message: error[E0210]: type parameter `C` must be used as the type parameter for some local type (e.g. `MyStruct<C>`) --> src/lib.rs:6:1 | 6 | impl<C> Index