Struct

From Computer Science Wiki
Revision as of 09:11, 4 February 2023 by Bmackenty (talk | contribs) (→‎References)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A "struct" is a composite data type that groups together zero or more values with different data types into a single unit. It is used to represent a single object made up of multiple pieces of data, such as a point in 2D space (x, y), or a record in a database (name, address, age, etc.). Each piece of data within a struct is called a "field". Structs can be found in many programming languages, including C, C++, and Rust. They provide a way to bundle related data into a single entity, making it easier to pass data around as a single argument, store it in a collection, or return it as a single result from a function.[1]

Here are two examples of using structs in Rust:

A struct representing a point in 2D space:

struct Point {
    x: f32,
    y: f32,
}

A struct representing a person's information:

struct Person {
    name: String,
    age: u8,
    address: String,
}

References

  1. ChatGPT