traits

What is the difference between traits in Rust and typeclasses in Haskell?

孤街浪徒 提交于 2019-11-28 15:50:19
Traits in Rust seem at least superficially similar to typeclasses in Haskell, however I've seen people write that there are some differences between them. I was wondering exactly what these differences are. At the basic level, there's not much difference, but they're still there. Haskell describes functions or values defined in a typeclass as 'methods', just as traits describe OOP methods in the objects they enclose. However, Haskell deals with these differently, treating them as individual values rather than pinning them to an object as OOP would lead one to do. This is about the most obvious

Understanding Traits and Object Safety

雨燕双飞 提交于 2019-11-28 14:00:37
I am struggling with the basics of object safety. If I have this code struct S { x: i32 } trait Trait: Sized { fn f(&self) -> i32 where Self: Sized; } fn object_safety_dynamic(x: Trait) {} I receive fn object_safety_dynamic(x: Trait) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `traits::Trait` cannot be made into an object = note: the trait cannot require that `Self : Sized` When adding / changing :Sized as the trait's inheritance or f 's bound I receive slightly different error messages. Could someone explain: Why does this particular example not work? The chapter Trait Objects states "So

Rust Trait object conversion

拥有回忆 提交于 2019-11-28 13:59:33
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 trait. I think this may be an instance of the compiler enforcing unnecessary constraints and I've

Why can't I add a blanket impl on a trait with a type parameter?

天涯浪子 提交于 2019-11-28 13:41:46
Consider these two traits: pub trait Foo { fn new(arg: u32) -> Self; } pub trait Bar<P>: Foo { fn with_parameter(arg: u32, parameter: P) -> Self; } I'd like to add the blanket impl: impl<T: Bar<P>, P: Default> Foo for T { fn new(arg: u32) -> Self { Self::with_parameter(arg, P::default()) } } But I get the compiler error: error[E0207]: the type parameter `P` is not constrained by the impl trait, self type, or predicates --> src/lib.rs:9:17 | 9 | impl<T: Bar<P>, P: Default> Foo for T { | ^ unconstrained type parameter I think I get this error because I'm violating trait coherence rules, but I

How to implement a trait for a parameterized trait

别说谁变了你拦得住时间么 提交于 2019-11-28 13:39:15
I have a design issue, when using something like : trait MyTrait<K: OtherTrait> { ... } impl<K: OtherTrait, M: MyTrait<K>> AnyTrait for M { ... } I cannot implement trait for this trait due to E207 error ("the type parameter K is not constrained by the impl trait, self type, or predicates"). Finding no way to get rid of this error, I apply this not-so-good-looking workaround (verbose and struct with no intrinsic value): use std::fmt; use std::marker::PhantomData; pub trait MyTrait<K: fmt::Display> { fn get_some_k(&self) -> Option<K>; } /* // This is my target impl but results in E207 due to K

Traits as a return value from a function [duplicate]

不想你离开。 提交于 2019-11-28 13:28:08
This question already has an answer here: How do I return an instance of a trait from a method? 3 answers I have two enums, NormalColour and BoldColour , both of which implement the Colour trait. They contain Blue , BoldGreen , and so on. I'd like to return values of both of these types from the same function, treating them as though they're just a Colour value, calling the paint function on the result, but I can't find a way to coerce the Rust complier into doing this for me. I'd like to be able to write something like this: pub trait Colour { fn paint(&self, input: &str) -> String; } fn file

Specify `Fn` trait bound on struct definition without fixing one of the `Fn` parameters

元气小坏坏 提交于 2019-11-28 13:27:59
I have a struct that contains a function object: struct Foo<F> { func: F, } I want to add an Fn trait bound to the struct definition. The problem is: I do care about the first parameter (it has to be i32 ), but not the second one. What I actually want to write is something like this: struct Foo<F> where ∃ P so that F: Fn(i32, P), { func: F, } So in English: the type F has to be a function that takes two parameters, the first of which is an i32 (and the second one can be anything). The syntax above is obviously not valid. I thought about three potential solutions: The for<> syntax won't help

Using impl Trait in Trait definition

家住魔仙堡 提交于 2019-11-28 13:16:52
Is it at all possible to define functions inside of traits as having impl Trait return types? I want to create a trait that can be implemented by multiple structs so that the new() functions of all of them returns an object that they can all be used in the same way without having to write code specific to each one. #![feature(conservative_impl_trait)] trait A { fn new() -> impl A; } However, I get the following error: impl Trait not allowed outside of function and inherent method return types Is this just a limitation of the current implementation of impl Trait in Rust or am I using it wrong?

Multiple inheritance using classes

我与影子孤独终老i 提交于 2019-11-28 13:10:24
Is it possible to extend only some specific part from multiple classes? Example: class Walker { walk() { console.log("I am walking"); } // more functions } class Runner { run() { console.log("I am running"); } // more functions } // Make a class that inherits only the run and walk functions You can pick and choose which methods from other classes you want to add to an existing class as long as your new object has the instance data and methods that the methods you are adding expect to be on that object. class Walker { constructor() {} walk() { console.log("I am walking"); } // more functions }

Rust generics: Expected <T> found <Foo>

♀尐吖头ヾ 提交于 2019-11-28 12:39:38
问题 I'm trying to use generics but I don't master that topic well enough and I get this error: error: mismatched types: expected `book::mdbook::MDBook<R>`, found `book::mdbook::MDBook<renderer::html_handlebars::HtmlHandlebars>` (expected type parameter, found struct `renderer::html_handlebars::HtmlHandlebars`) [E0308] This is the relevant code pub struct MDBook<R> where R: Renderer { title: String, author: String, config: BookConfig, pub content: Vec<BookItem>, renderer: R, } impl<R> MDBook<R>