Is this struct inheritance in Rust?

I'm reading this code: https://github.com/meh/rust-ffmpeg/blob/0bcd4550b85580d02599d84b312251d930d1802f/src/codec/decoder/decoder.rs#L9

and I found:

pub struct Decoder(pub Context);

What does it mean? I've never seen it before, and couldn't find anything advanced about struct syntax on google.

This is a tuple struct, containing only one field. This pattern is widely used to either restrict or expand the list of possible actions with the struct.

Tuple Struct are like named tuple, there are declared just like a struct but use parenthesis instead of curly braces. popular example uses Color RGB

struct Color(u8, u8, u8);

you can access their members using their index value. you can also see them as structs without field names just types. Tuple_Structs can also have functions just like a regular struct using impl keyword.

struct Color(u8, u8, u8);

impl Color {
    fn get_red(&self) -> u8 {
        self.0
    }
}

fn main() {
   let _color_red = Color(255, 0, 0);
   let _color_green = Color(0, 255, 0);
   let _color_blue = Color(0, 0, 255);

   // or a mix
   let sky_blue = Color(135, 206, 235);

   // you can also unpack similar to python
   let Color(red, green, blue) = sky_blue;

   println!("{} {} {}", red, green , blue);


   // Tuple- like struct can also have functions just like struct
   println!("RED {}", sky_blue.get_red());

}

When looking up part of Rust's syntax or a particular feature, it's always a good idea to check The Rust Reference. For example, the struct page shows the syntax in a pseudo-BNF form and mentions the different ways they can be declared.

A struct is a nominal struct type defined with the keyword struct .

An example of a struct item and its use:

struct Point {x: i32, y: i32}
let p = Point {x: 10, y: 11};
let px: i32 = p.x;

A tuple struct is a nominal tuple type, also defined with the keyword struct . For example:

struct Point(i32, i32);
let p = Point(10, 11);
let px: i32 = match p { Point(x, _) => x };

A unit-like struct is a struct without any fields, defined by leaving off the list of fields entirely. Such a struct implicitly defines a constant of its type with the same name. For example:

struct Cookie;
let c = [Cookie, Cookie {}, Cookie, Cookie {}];

is equivalent to

struct Cookie {}
const Cookie: Cookie = Cookie {};
let c = [Cookie, Cookie {}, Cookie, Cookie {}];

The precise memory layout of a struct is not specified. One can specify a particular layout using the repr attribute.

As an aside, what you are seeing is often referred to as the Newtype Pattern.

This is where you'll take an existing type and wrap it in your own type to either add application-specific methods, provide encapsulation, or mediate access (e.g. you have invariants that must be upheld, and letting people access the item directly could violate them).

Depending on the scenario you may also make the field pub so users have access to the inner type. Implementing Deref may also make sense if a pointer to your newtype should also be considered as a pointer to the inner type, although using this to implement a poor man's version of inheritance is frowned upon and will often lead to frustration because you are using a tool for something it's not intended for (Deref is for implementing something that behaves like a pointer, not is-a relationships).

3 Likes