rustClass attribute in Rust lang
Class attributes in Rust lang are used to store data associated with a particular type. They are declared using the keyword static and can be accessed using the double colon (::) operator.
Code example:
struct Point {
x: i32,
y: i32,
}
impl Point {
// Declare a static attribute
static origin: Point = Point { x: 0, y: 0 };
}
fn main() {
// Access the static attribute
println!("The origin is at ({}, {})", Point::origin.x, Point::origin.y);
}
Output
The origin is at (0, 0)
Code explanation of code parts:
struct Point: This declares a struct type named Point which contains two fields, x and y.static origin: Point = Point { x: 0, y: 0 };: This declares a static attribute named origin of type Point and assigns it the value of a Point instance with x and y set to 0.Point::origin: This accesses the static attribute origin.println!("The origin is at ({}, {})", Point::origin.x, Point::origin.y);: This prints out the origin's x and y coordinates.
Helpful links:
More of Rust
- Regex example to match multiline string in Rust?
- How to use regex captures in Rust?
- How to use binary regex in Rust?
- How to print a Rust HashMap?
- How to use regex to match a double quote in Rust?
- How to replace a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to match a URL with a regex in Rust?
- How to use regex with bytes in Rust?
- How to make regex case insensitive in Rust?
See more codes...