2

My program works as expected with my local machine. It prints random lines from a text file in the vein of the Emacs "Spook" amusement (M-x spook). The text file is located in src/spook.lines.

C:\Users\datan>spooks
domestic Eiffel Tower Euzkadi Ta Askatasuna Euzkadi Ta Askatasuna minefield
C:\Users\datan>spooks
Uzi White House secret Sivi Vukovi divers
C:\Users\datan>spooks
Hizb-i-Islami smallpox US Airways SWAT plague
C:\Users\datan>

But if I publish it as a crate and install it, the text file is not found (because it is not in src/spook.lines if the program is installed from crates.io):

C:\Users\datan>docker run -it rust bash
Unable to find image 'rust:latest' locally
latest: Pulling from library/rust
16ea0e8c8879: Pull complete
50024b0106d5: Pull complete
ff95660c6937: Pull complete
9c7d0e5c0bc2: Pull complete
29c4fb388fdf: Pull complete
ee0ab7fd0ac4: Pull complete
Digest: sha256:d8e2b124d6f4fcdf977bf7e6cfbda87fa5061b0c1933742699ee5131444217c9
Status: Downloaded newer image for rust:latest
root@137111b4d94c:/# cargo install spooks
    Updating crates.io index
  Downloaded spooks v0.1.0
  Downloaded 1 crate (19.0 KB) in 1.60s
  Installing spooks v0.1.0
  Downloaded getopts v0.2.21
  Downloaded rand v0.7.2
  Downloaded rand_chacha v0.2.1
  Downloaded easy_reader v0.5.0
  Downloaded getrandom v0.1.13
  Downloaded rand_core v0.5.1
  Downloaded unicode-width v0.1.7
  Downloaded libc v0.2.66
  Downloaded c2-chacha v0.2.3
  Downloaded fnv v1.0.6
  Downloaded cfg-if v0.1.10
  Downloaded ppv-lite86 v0.2.6
   Compiling libc v0.2.66
   Compiling getrandom v0.1.13
   Compiling cfg-if v0.1.10
   Compiling ppv-lite86 v0.2.6
   Compiling fnv v1.0.6
   Compiling unicode-width v0.1.7
   Compiling getopts v0.2.21
   Compiling c2-chacha v0.2.3
   Compiling rand_core v0.5.1
   Compiling rand_chacha v0.2.1
   Compiling rand v0.7.2
   Compiling easy_reader v0.5.0
   Compiling spooks v0.1.0
    Finished release [optimized] target(s) in 22.15s
  Installing /usr/local/cargo/bin/spooks
   Installed package `spooks v0.1.0` (executable `spooks`)
root@137111b4d94c:/# spooks
(no output because src/spook.lines is not there)

The way I include the file is:

let file = File::open("src/spook.lines")?;

Why can I not just tell the cargo/crate packaging to include the text file? What is the preferred way? I mean even if I include the file in a certain directory then it would not be in the PATH of the execution because the program might be run from anywhere.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • One possible workaround is to `include!` it. – Inline Dec 18 '19 at 07:29
  • @Inline Yes I started try that, but the `easy_reader` crate I use requires a file object and not a string, so that would mean create a "dummy" file in the local directory and possibly have to clean and remove it every time the program is run(?) – Niklas Rosencrantz Dec 18 '19 at 07:34
  • 1
    According to [its documentation](https://docs.rs/easy_reader/0.5.0/easy_reader/struct.EasyReader.html) `easy_rider` only requires a `Read + Seek`, so you can `include!` your text and use a [`Cursor`](https://doc.rust-lang.org/stable/std/io/struct.Cursor.html) to access it. See also: https://stackoverflow.com/a/32675033/5397009 – Jmb Dec 18 '19 at 07:44
  • Hi! I think your question is basically the same as [this one](https://stackoverflow.com/questions/39888534/how-do-i-access-assets-included-in-a-rust-cargo-project-installed-via-cargo-ins). While the one I linked does not have a satisfying answer (yet), do you agree that we should close yours as duplicate of the one I linked? – Lukas Kalbertodt Dec 18 '19 at 07:54
  • @LukasKalbertodt This answer by you seems to be the solution https://stackoverflow.com/a/41069910/108207 – Niklas Rosencrantz Dec 18 '19 at 08:49

1 Answers1

0

You can use the include_str! macro.

fn main() {
    let my_str = include_str!("spanish.in");
    assert_eq!(my_str, "adiós\n");
    print!("{}", my_str);
}

https://doc.rust-lang.org/std/macro.include_str.html

  • This is not an answer. This is just a workaround. – Inline Dec 18 '19 at 08:12
  • Hi there! Your answer should explain how to solve the `easy_reader` problem that came up in the comments. But I'd much prefer if we would close this question in favor of [this one](https://stackoverflow.com/questions/39888534/how-do-i-access-assets-included-in-a-rust-cargo-project-installed-via-cargo-ins). There, `include_str` is already mentioned. – Lukas Kalbertodt Dec 18 '19 at 08:16