traits Questions

3

Solved

I was writing some code and had a trait with a method that takes self by value. I want to call this method on a Box'd trait object (consuming the Box and its value). Is this possible? If so, how? I...
Gangue asked 12/12, 2020 at 3:46

1

Solved

I am trying to create an error type with a blanket From implementation for any Error, however since this type is itself an Error I am getting conflicts: pub struct ApiError(pub i64, pub String); i...
Spermato asked 1/10, 2022 at 23:47

3

Solved

I have a trait called Sleep: pub trait Sleep { fn sleep(&self); } I could provide a different implementation of sleep for every struct, but it turns out that most people sleep in a very sma...
Krissy asked 25/3, 2015 at 13:5

1

Solved

Say I have this code: pub trait A {} pub trait B {} pub trait SomeBehavior { fn func() -> bool; } And I want to provide the blanket implementation for A and B like this: impl <T> SomeBe...
Schargel asked 20/9, 2022 at 6:46

0

I've come across a case where I have a generic that needs to satisfy two traits. pub struct FileReader<R: Read + Seek> { /* private fields */ } These are standard traits and I can find their...
Jarman asked 17/9, 2022 at 5:26

7

Solved

What is the best way to define constants that may be used by a number of classes within a namespace? I'm trying to avoid too much inheritance, so extending base classes is not an ideal solution, an...
Onondaga asked 23/6, 2014 at 3:19

6

Solved

Scenario: trait A { function calc($v) { return $v+1; } } class MyClass { use A; function calc($v) { $v++; return A::calc($v); } } print (new MyClass())->calc(2); // should print 4 ...
Ravenous asked 13/8, 2012 at 17:19

6

Solved

UPDATE: I am not alone in my pondering on this issue and it seems it is indeed a bug. See here. The day it is fixed is going to be a fantastic day! :) This started out as I love PHP traits! I'm ...
Albie asked 4/12, 2013 at 17:41

2

Solved

In a crate I write, I have a bunch of internal structs public to the user and that share some code. Some of the shared code is public, some is an internal implementation. To share efficiently the c...
Mcnalley asked 8/11, 2018 at 8:56

10

Solved

Instead of writing town->first I would like to write town->name. Inline named accessors (Renaming first and second of a map iterator and Named std::pair members) are the best solutions I have...
Adahadaha asked 21/5, 2011 at 19:54

2

Most patterns in Rust are captured by traits (Iterator, From, Borrow, etc.). How come a pattern as pervasive as len/is_empty has no associated trait in the standard library? Would that cause probl...
Boanerges asked 28/2, 2020 at 10:10

2

I want to create a new iterator method such as: let test_data = vec![1,2,3,1,1,1,1]; let indexes_with_val_1 = test_data.iter().find_all(|element| element == 1).unwrap(); assert_eq!(indexes_with_va...
Gymnastics asked 29/6, 2022 at 15:31

2

My app uses custom color themes but iOS13 users can opt into following dark mode. I thought I could simply update my colors in the ViewController's traitCollectionDidChange() but for some reason, ...
Tub asked 25/10, 2019 at 11:39

1

Relevant code is this: use chrono::{DateTime, Utc}; // 0.4.10 use postgres::{Client, NoTls, Config, types::{FromSql, Type}}; //... for row in client.query("SELECT * FROM pricetable;", &am...
Ewold asked 12/6, 2022 at 16:41

3

Solved

Consider the situation where there are multiple classes that all need to have access to the configuration storage mechanism, but cannot be extended from a base class because of the architectuur of ...
Indigence asked 5/2, 2017 at 16:10

2

In rust I would like to have a vec containing items which implement 2 traits. However when I try to implement it like so, I get the error only auto traits can be used as additional traits in ...
Solarism asked 17/4, 2022 at 20:21

2

Solved

I can define an associated function foo for an array like this: pub trait T { fn foo(); } impl<X> T for [X; 2] { fn foo() { panic!("Whatever") } } But how do I now call this fun...
Ichthyosis asked 10/4, 2022 at 16:47

2

Solved

How to check if a struct implement some trait and calculate boolean result. For example: struct Foo; struct Bar; trait Qux {} impl Qux for Foo {} const fn is_qux<T>() -> bool { ... } ass...
Belay asked 2/4, 2022 at 20:4

3

I read through the trait documentation and found a neat definition for using traits on structs. Is it possible to use traits on enum types? I have seen answers that say no, but they are 3 years old...
Harrell asked 28/7, 2018 at 1:41

4

Solved

I want to implement the Iterator trait for a struct which contains an iterable field. Iterating over my struct should yield the same results obtained iterating over the field. This is what I'd like...
Beadledom asked 6/7, 2021 at 22:24

4

Solved

Regular float literals do not work: extern crate num_traits; use num_traits::float::Float; fn scale_float<T: Float>(x: T) -> T { x * 0.54 } fn main() { let a: f64 = scale_float(1.23)...
Gannet asked 8/6, 2018 at 20:22

1

Solved

I want to implement the frequent math multiplication of a vector by a scalar: k * v = (k * v0, k * v1, k* v2..). The code is the following: #[derive(Debug)] struct V(Vec<i32>); impl std::ops...
Especially asked 5/2, 2022 at 17:58

3

Solved

I want to know if there is a solution on how to unit-test a PHP trait. I know we can test a class which is using the trait, but I was wondering if there are better approaches. Thanks for any advi...
Mink asked 26/6, 2015 at 22:43

4

Solved

I find some confusing use of trait in some unittesting code, such as: trait MyTrait { val t1 = ... //some expression val t2 = ... //some expression } And then instantiate the trait using new a...
Escurial asked 28/4, 2013 at 3:7

2

Solved

In Salsa, there is a higher-ranked trait bound on a trait. I've seen HRTBs on function definitions but not on a trait. What does it mean? pub trait Query: Debug + Default + Sized + for<'d> Qu...
Santonin asked 2/1, 2022 at 16:23

© 2022 - 2025 — McMap. All rights reserved.