Writing a strings with concatenation and Insertion into a file

I know my following program is not correct, I just want to share to understand my questions;

use rand::Rng;
use std::fs::File;
use std::io::prelude::*;

fn main() -> std::io::Result<()> {
    let mut rng = rand::thread_rng();
    let mySecNo: u8 = rng.gen();
    let myString = "'{"jsonrpc":"2.0","method":"getBcCount","params":[],"id":1}'";
    let myFinalString ='{"jsonrpc":"2.0","method":"getBcCount","params":[mySecNo],"id":1}';
     let mut file = File::create("foo.txt")?;
    file.write(mystr)?;
   
    Ok(())
}

I have following problems;

  1. How to declare a string with this value let mut myString = '{"jsonrpc":"2.0","method":"getBcCount","params":[],"id":1}'

  2. I want to concatenate a newly generated no. mySecNo= ‘0x04fd’ . (random number) to already defined string myString. And also I want to know how to generate random Hexadecimal numbers.

  3. How to insert mySecNo into myString at some specific location i.e. "params":[mySecNo], which become let myFinalString = '{"jsonrpc":"2.0","method":"getBcCount","params":[mySecNo],"id":1}'

  4. At the end , how to write my myFinalString into a file

Try this:

use std::fs::File;

// Note: if you need upper-case hex , you can use :X instead in your format string
let my_file = File::create("my_file.txt")?;
let my_string = String::from(format!(r#"'{"jsonrpc":"2.0","method":"getBcCount","params":[{:x}],"id":1}'"#, my_sec_no));
my_file.write(my_string);

I am typing on mobile, so I can’t check for absolute correctness, but the gist of it is what you should do.

1 Like

Thanks so much, it seem to be good, but I received this error;
error: invalid format string: expected‘}’, found’"'.However I removed it through compiler help by putting extra { } i.e. let myString = String::from(format!(r#"'{{"jsonrpc":"2.0","method":"getBcCount","params":[{:x}],"id":1}}'"#, mySecNo));
and also I received this error

error[E0308]: mismatched types
file.write(myString)?;
note: expected type `&[u8]`
              found type `std::string::String`

the above error I removed by file.write(myString.as_bytes())?;
Now I want to know that how to make number auto increment which is appear after the keyword id:, i.e. '{"jsonrpc":"2.0","method":"getBcCount","params":[],"id":1}' '{"jsonrpc":"2.0","method":"getBcCount","params":[],"id":2}'
'{"jsonrpc":"2.0","method":"getBcCount","params":[],"id":3}'

Is the code inside a function which is only called once per execution, and that function should loop for however many times you want, or do you want it to remember its last value across multiple function calls? If the latter, I don’t know how in Rust. In C++, I would’ve used a local static variable.

If the former, do you know how many times the code will run? If you know, you could use a for loop like this:

for i in 1..=MAX {
    my_string = format!(r#"’{{“jsonrpc”:“2.0”,“method”:“getBcCount”,“params”:[{:x}],“id”:{}}}’"#, my_sec_no, i);

For an unknown amount of iterations, you should manage your own loop counter. The format string is the same, though. Just add {} in the position where you want the counter, and add a parameter to the format! macro.

1 Like

Knowing how to write such escaped strings is useful, but you might also like the json! macro of serde_json crate, which allows you to write json validated at compile time, and proper json escaping of interpolated variables:

https://docs.serde.rs/serde_json/macro.json.html

1 Like

You saved my life… thanks a lot

Could you give me one more help ? I shall be very thankful to you. I have to generate random value of 32 BYTE of size for my ‘params’ variable it should be look like this ‘88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b’. So how to generate it ?.. I am doing the something like this , but is generate 8 BYTE data
let mut rng = rand::thread_rng();
let mySecNo: u64 = rng.gen();

Generate four different random 8 byte numbers and stick them together?

1 Like

thanks