Attribute not found in a struct getter method

Hello,

I want to write a struct with getter methods. I get an error that the Self constructor can only be used with tuple or unit structs. Can someone explain what this means? Thanks!

//common.rs
pub enum OpCode {
	OpConstant,
	OpReturn,
	Halt
}

pub struct CodeChunk {
	code:Vec<OpCode>,
	data:Vec<f64>,
	source_lines:Vec<i32>
}

impl CodeChunk {

	pub fn new() -> CodeChunk {
		Self {
			code:Vec::new(),
			data:Vec::new(),
			source_lines:Vec::new()
		}
	}

	pub fn get_code_chunk(&self) -> Vec<OpCode>{
		Self.code
	}
...
}

Self with the uppercase S refers to the type you're implementing (i.e. CodeChunk). What you want is the self variable, with the lowercase s

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.