pub struct LineNumber(pub u32);
Expand description
A distinct number type for line numbers, to prevent confusion with other numerical data.
Zero-indexed internally.
We use a 32-bit integer, so a file cannot have more than 4 billion
lines. This keeps the size of the struct small. It’s common to
have a lot of LineNumber
s when analysing large files, so the
struct size is more important than handling crazy big files.
Tuple Fields§
§0: u32
Implementations§
Source§impl LineNumber
impl LineNumber
Sourcepub fn display(self) -> String
pub fn display(self) -> String
Examples found in repository?
examples/simple.rs (line 15)
3fn main() {
4 let s = "foo\nbar\nbaz\n";
5 let s_lines: Vec<_> = s.lines().collect();
6
7 let line_positions = LinePositions::from(s);
8
9 let offset = 5;
10 let (line_num, column) = line_positions.from_offset(offset);
11
12 println!(
13 "Offset {} is on line {} (column {}), and the text of that line is {:?}.",
14 offset,
15 line_num.display(),
16 column,
17 s_lines[line_num.as_usize()]
18 );
19}
Sourcepub fn as_usize(self) -> usize
pub fn as_usize(self) -> usize
Examples found in repository?
examples/simple.rs (line 17)
3fn main() {
4 let s = "foo\nbar\nbaz\n";
5 let s_lines: Vec<_> = s.lines().collect();
6
7 let line_positions = LinePositions::from(s);
8
9 let offset = 5;
10 let (line_num, column) = line_positions.from_offset(offset);
11
12 println!(
13 "Offset {} is on line {} (column {}), and the text of that line is {:?}.",
14 offset,
15 line_num.display(),
16 column,
17 s_lines[line_num.as_usize()]
18 );
19}
Trait Implementations§
Source§impl Clone for LineNumber
impl Clone for LineNumber
Source§fn clone(&self) -> LineNumber
fn clone(&self) -> LineNumber
Returns a duplicate of the value. Read more
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for LineNumber
impl Debug for LineNumber
Source§impl From<u32> for LineNumber
impl From<u32> for LineNumber
Source§impl Hash for LineNumber
impl Hash for LineNumber
Source§impl Ord for LineNumber
impl Ord for LineNumber
Source§fn cmp(&self, other: &LineNumber) -> Ordering
fn cmp(&self, other: &LineNumber) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for LineNumber
impl PartialEq for LineNumber
Source§impl PartialOrd for LineNumber
impl PartialOrd for LineNumber
impl Copy for LineNumber
impl Eq for LineNumber
impl StructuralPartialEq for LineNumber
Auto Trait Implementations§
impl Freeze for LineNumber
impl RefUnwindSafe for LineNumber
impl Send for LineNumber
impl Sync for LineNumber
impl Unpin for LineNumber
impl UnwindSafe for LineNumber
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