Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Methods on structs

Open
#79 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
48/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
documentation

Research direction

Search the documentation for the quoted data types text and read the surrounding discussion of traits and impls. Update the explanation to distinguish methods from functions and include the provided struct example; done means the section no longer implies structs cannot have methods.

Written by the indexing model from the issue text.

Description

In the section on data types it states:

A rust struct is similar to a C struct or a C++ struct without methods. Simply a list of named fields. The syntax is best seen with an example:

This statement is misleading because it suggests that methods can not be defined for a struct, while methods can be defined via impl blocks. There is a brief mention of this possibility with:

Behaviour is defined by functions and those can be defined in traits and impls

However, this suggests the impls would define functions rather than methods, while in fact impls can define both.

For reference here is a short example outlining the type of functionality I am describing.

struct Example {
    foo: i16,
    bar: i16,
}

impl Example {
    fn baz(&self) -> i16 {
        self.foo + self.bar
    }
}

fn main() {
    let ex = Example {
        foo: 3,
        bar: -1,
    };
    println!("{}", ex.baz());
}

This will print 2. I believe a section on this functionality should be added as it is critical to creating useful abstractions.

Dominant language
Rust
Stars
3.9k
Forks
297
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nrc/r4cppp

All issues in nrc/r4cppp

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.