Answers to StackOverflow's top Rust programming questions explained
source link: https://www.youtube.com/watch?v=Flf4ezLWw1E
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Answers to StackOverflow's top Rust programming questions explained
If you're learning Rust, you've probably wanted answers to these questions. The video is a recorded live stream with lots of discussion.
Timeline
00:00 Welcome and introduction 02:29 What are the differences between String and &str? 18:40 Why do we need an ampersand (&) when dealing with str? 19:40 Language Server Protocol (LSP) support in Rust 21:40 How to create a function that accepts a String or a str 35:26 Why doesn't println! work in Rust's unit tests? 39:20 Why does the Rust compiler not optimize code assuming that two mutable references cannot alias? 44:40 How to disable unused code warnings in Rust? 47:20 How to I print the type of a variable in Rust? 52:07 Do I have any tips for starting with Rust programming from scratch? 55:50 What are "generics"? 58:17 What is a "trait"?
Here are direct links to the questions
SO questions tagged rust https://stackoverflow.com/questions/t... What are the differences between String and &str? https://stackoverflow.com/q/24158114/... Why doesn't println! work in Rust's unit tests? https://stackoverflow.com/q/25106554/... Why does the Rust compiler not optimize code assuming that two mutable references cannot alias? https://stackoverflow.com/q/57259126/... How to disable unused code warnings in Rust? https://stackoverflow.com/q/25877285/... How do I print the type of a variable in Rust? https://stackoverflow.com/q/21747136/...
About the speaker
Tim McNamara is the author of Rust in Action https://manning.com/books/rust-in-action. Follow Tim on Twitter https://twitter.com/timClicks and watch future tutorials live on Twitch https://www.twitch.tv/timclicks
Show lessShow more
16 Comments
What you said about strings isn't entirely accurate.
fn greet(name: &str) { println!("Hey!, {}", name); } Will accept both String and &str as a parameter without the need for a call to AsRef<str> as such:
fn main() { let jesus = String::from("Jesus"); let dave = "Dave"; greet(&jesus); // Note that you have to borrow a 'String' greet(dave); //Note that you don't have to borrow explicitly here }
Also, I strongly recommend (in fact I'd even go as far as to say) that you don't allocate Strings willy nilly just because. Rust is a systems language and, I'd argue, you are learning it or are interested in it because you want to make performant software, and String allocations (and Vectors, and Box, and HashMaps, etc.) are expensive and should be used with care, unless of course you don't care about performance or memory usage like this: fn greet<T: Into<String>>(name: T) { println!("Hey!, {}", name.into()); // potentially very expensive copy and allocation of data!!! Warning!!! } Or: fn greet<T: ToString>(name: T) { println!("Hey, {}", name.to_string()); // warning!!! The same as above!! } Cloning heap objects should not be your go to when writing software that's supposed to be performant, but as a beginner learning Rust I guess it's okay, but you should never reach for cloning objects unless absoultely necessary. In fact, I'd even say, that if you're cloning a lot in your codebase then you should try to see if there's a better way to structure your program than what you're currently doing.
References (or borrows in Rust lingo) should be the absolute first thing you reach for when chosing how to pass parameters (&T) and copy or clone cheap types like integers and floats and fat poitner types (like &str &dyn Trait, etc.).
And just to reiterate: Allocations? Not bad when needed but as systems level devs we should try to use the stack as much as we can and only touch the heap when absolutely necessary.
TL;DR: Don't allocate everything on the heap (Box, Vec, String, etc.) because it's easy and 'quick', reach for shared references (&T) first or static (stack arrays or a SmallVec, etc.) data structures before everything else.
Read more 1 week ago
Read more 1 week ago (edited)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK