Skip to main content

All Questions

Tagged with
0 votes
2 answers
110 views

how to work around object safe traits in rust

I'm working on writing a toy database implementation in Rust and I can't get around this trait requirement to be object safe. I basically want a way of representing a database table with columns of ...
some1and2's user avatar
  • 137
0 votes
0 answers
66 views

Impl trait method on generic and vec of generic within a struct

I'm trying to implement the approx crate traits for a struct and it's fine with the values that are f64, but I don't know how to implement for the Vec values. The struct is pub struct Body<T> { ...
thefrollickingnerd's user avatar
0 votes
1 answer
410 views

Rust trait method with default implementation self type is not what is expected

So I have this FileSystem trait in rust pub trait FileSystem: Debug + Send + Sync And in that trait I have this method here with a default implementation: fn location_with_sub_path(&self, ...
Jamzy01's user avatar
  • 327
0 votes
1 answer
422 views

Generic constructor for generic type in Rust

TL;DR: How do I guarantee I can construct arbitrary structs with WhateverStruct::new()? This: trait Constructable { fn new() -> Self; } struct Foo { bar: u8 /* whatever else it may ...
404 Name Not Found's user avatar
0 votes
1 answer
1k views

How to access struct fields from an implemented trait function in Rust?

So, following a tutorial online, I wanted to extend the functionality of a trait, Canine, using the name field from the struct the trait is implemented on: struct Animal { name: String, } impl ...
jkix's user avatar
  • 502
1 vote
1 answer
45 views

cast struct that not explictly impl a trait to the trait object?

If there is a struct NotTFoo has a method called say_hi that statifies TFoo trait but not explictly impl TFoo, is it possible to use NotTFoo as a TFoo trait object? // the trait trait TFoo { fn ...
user avatar
1 vote
1 answer
717 views

How to implement a trait for another trait in rust?

I have a trait called DataValue in my rust project, and all of my "DataValue" structs inherit from it and the trait itself is used to define shared functionality so that I can store "...
Jamzy01's user avatar
  • 327
2 votes
1 answer
250 views

How do I return a reference in rust, that is automatically dereferenced

Let's say I have the struct: struct Vector { data: [f32; 2] } Implementing the Index trait is easy: impl IndexMut<usize> for Vector { fn index_mut(&mut self, index: usize) -> &...
Tudor Szabo's user avatar
0 votes
1 answer
112 views

Extracting varying, parameterized 'new' methods to trait

I'm new to Rust and just bumped into this issue. I have many different structs, that all implement their own new methods, with a varying number of input parameters and types. Every new method does ...
Niek Peters's user avatar
3 votes
1 answer
805 views

How do I have a trait as a owned field of a struct?

I'm fairly new to Rust, I was tying to implement a struct that could have different structs that implemented a common trait as a field In traditional programming languages with garbage collector I ...
Alessandro Tornesi's user avatar
0 votes
0 answers
49 views

Implementing Struct for Trait: cannot infer type

Getting the following error: error[E0283]: type annotations needed --> src/client.rs:35:30 | 35 | println!("client: {:?}", Command::add_task("test", 10)); | ...
Nikk's user avatar
  • 7,871
-1 votes
1 answer
119 views

Add a member in a struct that implements a trait specified

Let's say I have this struct in Rust: // Trait that allows objects that // implement it to return themselves // to the caller. trait ReturnsSelf { fn get_self(self) -> Self { self } ...
AggelosT's user avatar
  • 148
-1 votes
1 answer
357 views

Rust not finding method of trait struct because of lifetime parameters [duplicate]

I was trying to do simple struct with few traits following examples on the internet an came across the following problem. //My struct in framebuffer.rs pub struct HighLevelFrameBuffer<'a> { ...
The amateur programmer's user avatar
0 votes
1 answer
60 views

How to lifetime annotate From trait in rust when indexing vector

I'd like to use the From trait to extract values from a json input into my struct CreateUser. If I extract the full etc vector as etc: Vec<String> it works fine, but I only need the second field ...
fl_'s user avatar
  • 162
0 votes
1 answer
82 views

Idiomatic way to implement interchanged trait bound

I have a problem with how to force a trait bound on a struct. The struct will not make sense without the trait and therefore I want to enforce that user-defined model implement PricingModel. The issue ...
adaptive-alexander's user avatar

15 30 50 per page
1
2 3 4 5