3 ms·
since we know that Sha256::new() will always produce 'valid' characters for utf8 you can just do let my_rust_string = str::from_utf8(result).unwarp() If
by unionpivo 7y ago
since we know that Sha256::new() will always produce 'valid' characters for utf8 you can just do
let my_rust_string = str::from_utf8(result).unwarp()
If there is a possibility of an error, (you read it from file, network ...) you can do:
let my_rust_string = match str::from_utf8(result) {
Ok(str) => str,
Err(err) => panic!("Invalid utf8: {}", err),
};
Or you can do lossy conversion (will have that question mark on chars that it cant decode)
let my_rust_string = String::from_utf8_lossy(result);
edit: Ok so i read sha2 api wrong. result() is raw bytes and result_str() is hex encoded string. Above wont be much use for either. And others have already explained what to use.
I just stared programming in rust several months ago, and that was my pain also, since where i live we still have some other encoding in use.