How to read a string with escape chars from a file?

Hi all,

I am using the Config crate [1] to get paths from input file. Since I am using windows, the paths are usually with \ and when I copy paste these paths into the input file, I get a panic "invalid escape character in string: R at line 2 column 13

I can skip this error by replacing all \ to \\ but that is a pain. Is there anyway I can avoid this?

My sample toml is,

	a = "c:\example\a.txt"
	b = "c:\example\b.txt"
	c = "c:\example\c.txt"
	d = "c:\example\d.txt"

[1] config - Rust Package Registry

No, because that's not valid TOML then. You should indeed replace occurrences of literal \ with \\. You can use any simple text editor to perform that operation.

Also, just use forward / on Windows, too. Windows accepts Unix-style paths, and everyone will have an easier time with those.

1 Like

I believe switching to single quotes will do the trick here.