Knowledge Garden

Search

Search IconIcon to open search

Structs in Rust

Last updated Dec 1, 2022 Edit Source

# Structs

in other languages there are classes, in Rust there are Structs

structs can have

structs should be in capital cammelcase

1
2
3
4
struct RedFox {
	enemy: bool,
	life: u8,
}

you can end last field with a comma too.

Instantiating a struct is straight forward but verbose. you need to call it and set a value for each field.

to make this easier you can use a constructor, these are defined in an implementation

1
2
3
4
5
6
7
8
impl RedFox {
	fn new() -> self {
		self {
			enemy: true,
			life: 70,
		}
	}
}

fn new => is an associate function. Because it doesn’t have a form of self as first parameter. In other languages you might call this a class method. with new being the conventional name used to instantiate.

1
let fox = RedFox::new();

here the scope operators :: are used to access the method inside the struct.

no struct inheritance. = fixes OOP stuff