Posts

Showing posts from September, 2021

Simple Rust Mutation Relationship Diagram

Rust mutation can be somewhat confusing if your a beginner. Its similar to C++ way of doing things on where to put the asterisk (*) and ampersand (&) sign in variable declaration. Moving the asterisk sign and ampersand sign makes the declaration sometimes more mutable and also can make it less mutable. Here is a simple diagram on Rust mutation that I found on StackOverflow (SO). I can’t find the exact link to reference as this one is stored in my notes.

Converting Rust String To And From

Image
Rust &str and String is different in a sense that str is static, owned and fix sized while String can be dynamically allocated once and be converted to mutable to be appended. Most of the time you’ll be working with String on Rust when re-allocating and moving values between structs . There are times you may need to convert dynamic string to char bytes and static string. Here are ways to do it: From &str &str -> String has many equally valid methods : String::from(st) , st.to_string() , st.to_owned() . But I suggest you stick with one of them within a single project. The major advantage of String::from is that you can use it as an argument to a map method. So instead of x.map(|s| String::from(s)) you can often use x.map(String::from) . &str -> &[u8] is done by st.as_bytes() &str -> Vec<u8> is a combination of &str -> &[u8] -> Vec<u8> , i.e. st.as_bytes().to_vec() or st.as_bytes().to_owned() From Strin