3,605 questions
1
vote
3
answers
68
views
Can you emulate rust's Fn trait using type classes?
In Rust, there is no type for functions, but rather an Fn trait that looks something like this:
trait Fn<A: Tuple, R> {
fn call(self, args: A) -> R;
}
Then the type of a function can be ...
0
votes
0
answers
33
views
Implement a trait for multiple generic traits
I'm trying to implement a trait for Display and Debug traits, but rust responds with conflicting implementations.
Here is my code:
pub trait AsString {
fn as_string(self) -> String;
}
impl<...
1
vote
0
answers
34
views
Recursive traits for flattening multidimensional vectors
Suppose I we want to flatten vectors:
[0, 50, 100] -> [0, 50, 100]
[[0], [50, 50], 100] -> [0, 50, 50, 100]
[[[0]]] -> [0]
We can define a trait such as
pub trait FlattenVec<A> {
...
0
votes
0
answers
37
views
How to resolve possibly conflicting recursive Rust trait implementations?
With the following code, I am able to add functionality to existing types:
pub trait Action<RHS> {
fn action(self, rhs: RHS) -> Self;
}
impl Action<i8> for u8 {
fn action(self, ...
1
vote
1
answer
65
views
Why can't you implement an impl Trait type in rust
Say that I have the following trait:
trait SayHello {
fn say_hello(self) -> String;
}
Why can I then do this:
fn say_hello_twice(this: impl SayHello) -> String {
let said_hello = this....
1
vote
1
answer
63
views
Take default value if not present
Have trait like below
trait MyTrait
{
val country: String,
val state: String,
val commune: String = "nothing"
}
Now implementing MyTrait
case class ImplementTrait(
...
0
votes
0
answers
26
views
Is there a way to provide a derived implementation for traits that have a specific associated type?
I'm trying to write a blanket implementation of a trait for types that are another trait with a specific associated type.
That is, it is fully permissible to do this:
use std::{marker::PhantomData, ...
1
vote
1
answer
64
views
How to downcast a trait to struct in Rust
I have the following trait defined:
pub trait TItem {
// some functions
}
I have two structs TArray and TObject with:
impl TItem for TArray {
// some functions
}
impl TItem for TObject {
// ...
0
votes
0
answers
37
views
How to create a Vec of instances of a struct using const generics, while still using methods depending on the value of the generic in Rust?
I am trying to learn some Rust by creating a neural network from scratch.
I started by defining a Matrix struct using const generics in order to check for matrix size at compile time when performing ...
1
vote
0
answers
63
views
Trait bound does not propagate to sub traits
I would expect the code example below to work since the bound on trait C has an implicit bound, through Bs bound on A::I, on I being Copy. It doesn't work though and I'm trying to understand why. I ...
0
votes
0
answers
37
views
Trouble with how to constraint a lifetime on a trait method
I am trying to implement nom's InputIter trait on my custom input type for dealing with tokens instead of string. My input type is implemented as follows:
#[derive(PartialEq)]
struct TokenInput<'a&...
15
votes
2
answers
1k
views
Why constrain the unit type by a generic trait bound in a where clause (as in `where () : Trait<…>`)?
Today I've encountered a somewhat weird syntax - where ():
fn hex_primary<Stream, Context>(stream: Stream) -> Parsed<u8, Stream, Context>
where
(): IntRadixParse<Stream, Context, ...
3
votes
2
answers
106
views
How to write an abstraction in Rust with multiple implementations
In my application I want to have an EventPersister trait that defines an abstraction of persisting events, then have various implementations that, for example, persist events in memory, in the file ...
1
vote
0
answers
45
views
Rust - The Trait Bound `diesel::expression::select_by::SelectBy<User, Pg>
let user_result = users
.filter(username.eq(&login_data.username))
.select((id, username, password_hash, created_at))
.first::<User>(&mut conn);
I take this error ;
the trait bound (...
-1
votes
0
answers
24
views
Is there a way to avoid the code repetition in these trait implementations? [duplicate]
I've been practising polymorphism with traits, and decided to make a small function that allows rounding floats to n degrees of precision. Here is what I came up with:
trait Round {
fn roundpres(&...