ib RustBite Conversions
about
05/11/2022
RustBites - Conversions
Code Repository Code Examples Rust Bites Repo

Rust Bite - Conversions

most Rust conversions are explicit

1. Prologue

There are only two types of implicit conversions provided by Rust:
  1. Conversion from mutable to immutable, e.g., can pass a mutable reference to a function expecting an immutable reference.
  2. Smart pointer dereferencing, e.g., Box<T>, Rc<T>, Arc<T>, and RefCell<T> implement the Deref trait. That means code can use the T interface through smart pointers without any explicit code boilerplate because the compiler automatically dereferences smart pointers where types wouldn't otherwise match.
Rust has defined two conversion traits Into and From which many library types implement and user-defined types can do that as well.
let s = String::from("a string");
Rather than use Into trait directly, it is usually simpler to use the format macro:
let v = vec![1, 2, 3];
let vs = format!("{}",v);
With format you can use any of the formatting facilities provided for console IO.

2. Primitives

Primitive types are defined as part of the Rust language, as in C++, C#, ... Here's a quick overview: RustBites DataStr. There is a nice description for conversions of primitives in Cheats.rs. It's all you need.

3. Strings

Rust provides a std::String type that represents a collection of utf-8 characters. A utf-8 character may occupy from 1 to 4 bytes. That means that Rust can represent languages that use diacritics, and script characters, e.g., Sanskrit, Hanzi, Arabic, ... It also means that Strings cannot be indexed. For that, Rust provides the chars() iterator. Each Rust String has a stack-based control block and a heap-based collection of characters, as shown in RustBites Data. Rust also provides the str type used to represent literal strings: "a literal string". We usually encounter this type behind a reference, e.g.: let s: &str = "a literal"; Rust intends to make interoperating with C language code straight-forward using its std::ffi (foreign-function interface) facility. for that it introduces the types OsString and OsStr, like String and str, respectively, but using the platform string encoding. It also provides PathBuf and Path which wrap OsString and OsStr with added methods for handling directory paths. The file system API, std::fs uses both in many of its function and method calls. There is a nice description for conversions of strings in Cheats.rs. It's almost all you need. Many types can be converted to strings using the format! macro. That allows you to use the same formatting used for console IO, but get that as a String

2. Other Types

There are too many combinations to address here. As I work on interesting examples I'll add some here.

3. References:

LinkDescription
Cheats.rs Very comprehensive collection of Rust facts in brief code snippets and comments.
Cheats number-conversions Conversions of primitive types.
Cheats string-conversions Conversions between String, &str, OsString, OsStr, PathBuf, and Path.
Cheats string-output Convert String contents for formatted output.
  Next Prev Pages Sections About Keys