Skip to content

Tarik Billa

  • Web Development
    • html
    • vue.js
    • laravel
    • css
    • javascript
    • jquery
    • node.js
    • php
    • asp.net
  • Programming
    • python
    • java
    • c
    • c++
    • c#
  • git
  • android

trait-objects

The trait cannot be made into an object

May 28, 2023 by Tarik

You can either add a type parameter to your struct, as in Zernike’s answer, or use a trait object. Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. A trait object uses dynamic dispatch so it lets you … Read more

Categories generics Tags dynamic-dispatch, generics, rust, trait-objects, traits Leave a comment

Sending trait objects between threads in Rust

March 30, 2023 by Tarik

It’s possible. You can add a Send constraint to a trait object like this: let foo = Box::new(Foo { foo: 1 }) as Box<dyn Bar + Send>; let (tx, rx): (Sender<Box<dyn Bar + Send>>, Receiver<Box<dyn Bar + Send>>) = channel();

Categories rust Tags rust, trait-objects Leave a comment

What makes something a “trait object”?

February 2, 2023 by Tarik

You have trait objects when you have a pointer to a trait. Box, Arc, Rc and the reference & are all, at their core, pointers. In terms of defining a “trait object” they work in the same way. “Trait objects” are Rust’s take on dynamic dispatch. Here’s an example that I hope helps show what … Read more

Categories rust Tags rust, trait-objects, traits Leave a comment

Tarik Billa

Software Engineer
tarikbilla@gmail.com
+8801884414000
  • Reuse a hash in YAMLApril 17, 2024
  • Dockerfile: how to redirect the output of a RUN command to a variable?April 16, 2024
  • How to cd to a directory with spaces in the directory name?April 16, 2024
  • Maximum MIME type length when storing the type in a databaseApril 16, 2024
  • What is the difference between Unit, Integration, Regression and Acceptance Testing?April 16, 2024
© 2026 Tarik Billa