-3

I tried this code

static HOST_FILE: &'static [u8] = include_bytes!("C:\\Users\\Downloads\\cbimage.png");

fn main() {
     let host_str = std::str::from_utf8(HOST_FILE). unwrap();

     println!("Hosts are:\n{}", &host_str[..42]);
}

But it shows me an error: thread 'main' panicked at 'called Result::unwrap() on an Err value: Utf8Error { valid_up_to: 66, error_len: Some(1) }', src\main.rs:48:51 stack backtrace

  • 2
    Does this answer your question? [Is there any way to include binary or text files in a Rust library?](https://stackoverflow.com/questions/32748918/is-there-any-way-to-include-binary-or-text-files-in-a-rust-library) – Finomnis Dec 04 '22 at 08:25
  • @Finomnis I tried it but it gives me an error Utf8Error { valid_up_to: 0, error_len: Some(1) }' – kcbbcn otfemb Dec 04 '22 at 08:37
  • @Finomnis I tried the second example as well And the error appeared: byte index 42 is out of bounds of Code: static HOST_FILE: &'static str = include_str!("C:\\Users\\Desktop\\hello desktop. txt"); fn main() { println!("Hosts are:\n{}", &HOST_FILE[..42]); } The number 42 indicates what? – kcbbcn otfemb Dec 04 '22 at 08:47
  • 1
    Your comments are turning the question into another question. Open a new one, with a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), including your code, the error messages, and the text file. – kotatsuyaki Dec 04 '22 at 08:54
  • @Finomnis I'm currently with a single example that only contains this code static HOST_FILE: &'static[u8] = include_bytes!("/etc/hosts"); fn main() { let host_str = std::str::from_utf8(HOST_FILE). unwrap(); println!("Hosts are:\n{}", &host_str[..42]); } But it gives me an error! – kcbbcn otfemb Dec 04 '22 at 09:00
  • Please don't use comments to update your question, [edit] your question directly :) – Finomnis Dec 04 '22 at 09:00
  • *"But it gives me an error!"* - Please don't make statements like this without actually showing the error. Just knowing that it fails on your machine gives us no information we could help you with. – Finomnis Dec 04 '22 at 09:02
  • I guess .. don't use the `42`? I'm not sure why that's part of the other question, but **never** copy-paste code from the internet into your project that you don't understand! Thats one of the very basic rules of stackoverflow. **Always** understand and verify code you copy from the internet. There are trolls, and there were cases where people made other people `sudo rm -rf /`. – Finomnis Dec 04 '22 at 09:04
  • Well now I've modified my question, I'll look into issue 42 and make sure – kcbbcn otfemb Dec 04 '22 at 09:09
  • 1
    Why do you think a png file is valid utf8? – cafce25 Dec 04 '22 at 10:00
  • @cafce25 It's not utf8 but it wants to convert to pass include_bytes Am i right ? – kcbbcn otfemb Dec 04 '22 at 10:15
  • 1
    If it's not then why do you call `std::str::from_utf8` on it? – cafce25 Dec 04 '22 at 10:19
  • @cafce25 I've modified it now and removed std::str::from_utf8 But he did not add the image to the application why ? fn main() { let bytes = include_bytes! ("C: \\ Users \\ Downloads \\ cbimage.png"); println! ("{:?}" Byte) ; } – kcbbcn otfemb Dec 04 '22 at 10:24
  • Again, do NOT paste multi-line code in comments. – Finomnis Dec 04 '22 at 11:09
  • If you want to add to your question, you can [edit] it and add a tag like **EDIT:** in front of it. Like in this question: https://stackoverflow.com/questions/74665076/visual-studio-code-distorts-ansi-characters – Finomnis Dec 04 '22 at 11:19

1 Answers1

0
static IMAGE: &'static [u8] = include_bytes!("C:\\Users\\Downloads\\cbimage.png");

fn main() {
    println!("Image: {:?}", IMAGE);
}

Although I recommend using a path relative to your source code, like

static IMAGE: &'static [u8] = include_bytes!("../cbimage.png");

fn main() {
    println!("Image: {:?}", IMAGE);
}

And place cbimage.png next to your Cargo.toml.

Finomnis
  • 18,094
  • 1
  • 20
  • 27