pub struct Location {
pub file: PathBuf,
pub line: usize,
}Expand description
Location information for a node.
§Rust Book Reference
Chapter 10.2: Traits - Deriving Traits https://doc.rust-lang.org/book/ch10-02-traits.html#deriving-traits
§Educational Notes - Derive on Structs
This struct demonstrates deriving traits on a struct with multiple fields:
ⓘ
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Location {
pub file: PathBuf,
pub line: usize,
}How derive works for structs:
- Compiler checks that ALL fields implement the trait
PathBufimplementsDebug, Clone, PartialEq, Eq✓usizeimplementsDebug, Clone, PartialEq, Eq✓- Therefore,
Locationcan derive these traits
Generated PartialEq implementation:
ⓘ
impl PartialEq for Location {
fn eq(&self, other: &Self) -> bool {
self.file == other.file && self.line == other.line
}
}Why this matters:
- Locations can be compared:
loc1 == loc2 - Locations can be cloned:
loc.clone() - Locations can be debugged:
println!("{:?}", loc)
Fields§
§file: PathBuf§line: usizeImplementations§
Trait Implementations§
impl Eq for Location
impl StructuralPartialEq for Location
Auto Trait Implementations§
impl Freeze for Location
impl RefUnwindSafe for Location
impl Send for Location
impl Sync for Location
impl Unpin for Location
impl UnwindSafe for Location
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more