rust
December 12, 2018
AOC: Sharing code in Rust
After first puzzle in advent of code i found a function that can be reused:
use std::fs::File; use std::io::prelude::*; // TODO: Share between puzzles... fn read_puzzle_input(number: u8) -> String { let mut input = File::open(format!("./inputs/{}.txt", number)) .expect("Puzzle input not found..."); let mut text = String::new(); input .read_to_string(&mut text) .expect("cannot read input file"); return text; }
I going to move this function to separate file and then somehow reuse in all my solutions. Quick googling leads to "Using Modules to Reuse and Organize Code" of The Rust Book.
what happens if you have too many functions? Rust has a module system that enables the reuse of code in an organized fashion.
Nice! Two important things from first page:
- By default, functions, types, constants, and modules are private. The
pub
keyword makes an item public and therefore visible outside its namespace. - The
use
keyword brings modules, or the definitions inside modules, into scope so it’s easier to refer to them.
Rust has pretty flexible module layout and there are some rules for module organization. For my simple case i just made following steps:
- Moved function to external file `src/utils.rs`
- prefixed function with pub keyword: pub fn read_puzzle_input...
- and added small snippet to Cargo.toml:
[lib] name = "utils" path = "src/utils.rs"
Now in order to use functions from this library i need added crate declaration to top of my file and prefixed function call with utils namespace:
extern crate utils; fn part_one() { let input = utils::read_puzzle_input(1); let mut result: i32 = 0; for line in input.lines() { let num = line.parse::<i32>().unwrap(); result += num; } println!("Sum of freq adjustments: {}", result); }