Any nice guides that clear this up? I’m often finding myself not knowing what is appropriate to use.
well… the book is always good:
https://doc.rust-lang.org/book/
and i enjoyed rust by example:
https://doc.rust-lang.org/stable/rust-by-example/
I’d recommend thinking about it like this:
into
is like cast to, ‘from’ is the inverse.
I have a value 3. I want it to be a string. new_string from 3 is “3”.
I have a value “3” now. I want to cast it to int. “3” into int is 3.
struct Point {}
impl From<i32> for Point {
fn from(i: i32) -> Self {
println!("casting: {}", i);
Point{}
}
}
fn main() {
let x: i32 = 3;
let my_val: Point = x.into();
let my_val = Point::from(x);
}
try_into
just does this process but allows you to add success and failure conditions, returning the Result
type.
clone
is pretty important, because it allows you to avoid issues with borrowing:
so something like this:
fn foo(my_string: String) {
println!("{}", my_string);
}
fn main() {
let mut my_string = String::from("Hello");
my_string.push_str(" ");
my_string.push_str("world");
foo(my_string.clone()); // my_string would go out of scope here!!!!
println!("{}", my_string);
}
So you need to clone it if you want to use it in a function because it will “borrow” the value and free it once it goes out of scope in the function.
So strings are weird. I’d just remember that a String
is a buffer. str
is an immutable slice. When you declare a variable with quotes, it becomes an immutable slice, ie. (“TEST” becomes a reference to an immutable str), but sometimes you want to be able to mutate this, so you can create a mutable reference to this slice, a String.
Something’s that might help.
.into()
Usually changing from one type to another where a From trait has been implemented. Say you have a str and you need a String you can call into() because there’s an implementation that knows how to convert between them.
json and strings
When holochain needs to send some data somewhere it sends it as a JSON encoded String but what is really being sent is just a String. So you can think of it as 3 stages.
- You have some data as a rust type.
- You convert it to a JSON encoded data.
- Convert the Json to a regular String.
And obviously the reverse of the above when you’re receiving data.
try_into()
You would use this when you’re not sure if a conversion will work. For example when you convert some data from a message to a JSON object then you try to convert it to a rust type then you can’t know for sure it will work.
A JsonString is basically the same as a String but when know that the data is in the JSON format. So basically it conforms to the JSON standard but it’s not a rust type yet.
from_json() is taking some String data and converting it to a JsonString. So basically checking that it does fit the above standard.
clone() is saying you want to clone the memory. So make a new copy. Not just a copy of a reference to the same memory.
Often this comes up when you have a reference like &str.
Then you need to send it somewhere but you want to send the actual data not the reference so you call clone() to make a copy that you own.