On 05 Oct 2022 at 10:47p, apam pondered and said...
I'm trying to learn rust.. I'm stuck however and was hoping someone more familiar with the language could tell me where I'm going wrong...
Sure!
I have a json file located in "users/apam.json"
I have a function to save the userfile (that worked great) however
loading it is causing me problems...
if I load it with :
let d = fs::read_to_string("users/".to_owned() + &username + ".json")?;
So a couple of things here....
First of all, there's a Path library with PathBuf that you
should use for this, instead of building up a string.
Second of all, have you tried `assert!()`'ing that the
file pathname you create is actually what you expect?
I can't reproduce this locally, though. Copy/pasting
into `syncterm` is hard for reasons, but
: chandra; mkdir users
: chandra; echo hi > users/apam.json
: chandra; cat src/main.rs
use std::fs;
fn main() {
let username = "apam".to_string();
let filename = "users/".to_owned() + &username + ".json";
println!("{filename}");
let d = fs::read_to_string("users/".to_owned() + &username + ".json").unwrap();
println!("{d}");
}
: chandra; cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/apam`
users/apam.json
hi
: chandra;
where username is a string that contains "apam"
it fails with "os error, no such file or directory"
I think I'd try to capture the filename you think you
are creating and ensure that it matches what you expect,
either printing it, or in an `assert!()` (or, even better,
in a `#[test]`!). Also ensure you are in the directory
you think you in, since you're opening a relative path?
if however I do this:
let d = fs::read_to_string("users/apam.json")?;
it works...
(yes I'm practicing rust by writing a toy bbs)
Cool.
--- Mystic BBS v1.12 A47 2021/12/24 (Linux/64)
* Origin: Agency BBS | Dunedin, New Zealand | agency.bbs.nz (21:1/101)