#file-line #static-file #compile-time #read-file #line #file #read-line

include-lines

Macros for reading in the lines of a file at compile time

4 stable releases

1.1.2 Feb 17, 2023
1.1.1 Feb 16, 2023
1.0.0 Dec 31, 2022

#908 in Filesystem

Download history 54/week @ 2024-07-21 336/week @ 2024-07-28 309/week @ 2024-08-04 679/week @ 2024-08-11 888/week @ 2024-08-18 744/week @ 2024-08-25 712/week @ 2024-09-01 1249/week @ 2024-09-08 739/week @ 2024-09-15 147/week @ 2024-09-22 80/week @ 2024-09-29 88/week @ 2024-10-06 244/week @ 2024-10-13 734/week @ 2024-10-20 733/week @ 2024-10-27 1159/week @ 2024-11-03

2,875 downloads per month
Used in 2 crates

MIT/Apache

8KB

include-lines

Rust macros for reading in all lines from a file at compile time. This can be very useful for loading static data.

Examples

For the examples, there is a file file.txt in the same directory as the project's Cargo.toml file:

these
are
file
lines

Read in a file and store it an an array of type [&'static str]

use include_lines::include_lines;
let lines = include_lines!("file.txt");

For the example file, this expands to:

let lines = [
    "these",
    "are",
    "file",
    "lines",
];

Read in a file and store it an an array of type [String]

use include_lines::include_lines_s;
let lines = include_lines_s!("file.txt");

For the example file, this expands to:

let lines = [
    String::from("these"),
    String::from("are"),
    String::from("file"),
    String::from("lines"),
];

Get the number of lines in a file at compile time as type usize

use include_lines::count_lines;
let num_lines = count_lines!("file.txt");

For the example file, this expands to:

let num_lines = 4usize;

Create a static array from a file at compile time

You can use the static_include_lines! and static_include_lines_s! macros to initialize static text arrays at compile time:

use include_lines::{static_include_lines};
static_include_lines!(LINES, "file.txt");

For the example file, this expands to:

static LINES: [&str; 4] = [
    "these",
    "are",
    "file",
    "lines",
];

Dependencies

~1.5MB
~37K SLoC