How to get the textual representation of an IR instruction?

If ‘I’ is of type llvm::Instruction, we can print out the human-readable form (textual representation) of it by by, errs() << I;

I want the assign the exact same representation to a std::string to C type string. How can I do that?

There is raw_string_ostream which prints to a std::string.


Answering my own question, the solution will be:

std::string str;
llvm::raw_string_ostream rso(str);