Skip to content

Miscellaneous Rust

Programming Tips in Rust

Syntax

Restrict function access to a module scope
1
2
3
pub (in crate::module) func_name() -> Result<T> {
  ...
}

Printing

You can center and pad strings in Rust using the following formatting syntax:

{:#^10}

Example:

Printing a padded String
1
2
3
4
5
# To center the word TODO in a string and surround it by '#'s:
$> println!("{:#^10}", "TODO");

# Output will be three '#'s TODO and then three more '#'s.
###TODO###

Box

You can leak memory using Box::leak. This is useful when you want to instantiate something on the heap and know it's going to stick around for the lifetime of the application. This means you can get a 'static lifetime reference to the object and don't have to worry about the cost of deallocating the object during runtime.

Example:

Consume and Leak a Box
1
2
3
4
let x = Box::new(41);
let static_ref: &'static mut usize = Box::leak(x);
*static_ref += 1;
assert_eq!(*static_ref, 42);

See: Box::leak

Guides, Courses, and Books

Books

Frameworks

Web Frameworks

Example Code

The following resources are good examples of code written in Rust, or are samples of real world applications (no emphasis on good or bad here).

Aquascope

Tools

Sites, Blogs, and Newsletters

  • Aweesome Rust Repo Stats

    This cool site shows the stats of the most starred and active Rust repositories on GitHub. It's a great way to find out what's popular in the Rust community.

Debugging Tools

Cargo tools

Testing

Benchmarking