Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Skills:
ML Maths Basics80%
Key Takeaways
The video covers the basics of Rust programming language, including its test system, attributes, configuration, and conditional compilation, using tools like cargo and rust-lang.
Full Transcript
hi guys welcome to introduction to rust my name is tensor the next few videos will be projects that use all of the stuff that we've learned thus far in this video we're gonna talk about the test suite that rust has we're gonna be talking about conditional compilation and we're going to be talking about attributes so when you create a new cargo library project this is what you'll get inside of your live dot RS the reason why we get this automatically generated is so that we can start doing test-driven development now of course library projects do not have main functions so the way that we can test their functionality rather than putting it inside of a main function is by calling tests if you were to jump into your terminal and just type in cargo tests you'll see here that it will run the automatically created tests called eggworks and it will of course pass because there's nothing inside of the tests itself all right so let's break down what's going on here first we have what is called an attribute and this has a CFG flag which means it's a conditional compilation flag so the CFG annotation actually stands for configuration CFG links over to what's called conditional compilation and we conditionally compile this code based on the configuration that means that this entire block here from where this attribute appears to the end of the scope here we're only compiled when we called the test suite because we've labeled it with tests so then we have mod tests and we've already talked about the module system so we're creating a sub module inside of our main dot RS file here and then we've got another attribute here that's just called test and this basically tells the compiler that this function itself is a part of the test suite so attributes are metadata about pieces of Russ code for instance the derive attribute is one example that we've seen quite a bit to make a test function we just add this test attribute to the line before the FN declaration now of course we can run our tests with Cargo tests as we just saw and Russ builds the test Runner binary that runs the functions annotated with test attributes and then reports whether or not the tests pass or fails in our test suite has access to the test attribute it also has various macros that we can call inside of it and it also has should panic attribute that we can use so consider this example here we've created a new function annotated with test called check two inside of it we're running a macro called assert and we're checking to see if one plus one is equal to 2 the assert macro is provided by the standard library it's useful when you want to ensure some condition in a test evaluates to true we give the assert macro an argument that evaluates to a boolean so in this case we have a boolean here it'll either be true or false if the value is true the assert does nothing and the test passes so in this case this will be true so it'll do nothing and it will pass however if we pass the value of false to our certain macro then it will call a panic which will then cause the test to fail so for this case this will panic because 3 is not equal to 4 and it will cause the test to fail so let's take a look at that so we ran our test you'll see here that it says running three tests says test tests check two test tests it works and then test test test three and you'll see that the first two we get okay and then the last one we get failed and then it tells us what the failures are so it says thread penikett assertion failed so it tells us exactly where it failed and this is because of the assertion macro so because 3 does not equal for the assertion macro was given false and then it called the panic macro which then forced the program to panic and then of course down here we get our test result where we say failed and we say 2 passed 1 failed 0 ignored 0 measured etc in addition to being able to check that our code returns true assertions we can also check that our code handles error conditions as we expect so for instance if we actually want this test to fail then we can add another attribute here called should panic and you're right should panic underneath of the test attribute here and what this tells the compiler is that this should panic and that that is actually a pass for us and you'll see here if we run Karger test again we get 3 passing tests this should panic attribute after the tests attributed before the test function this attribute makes the test passed if the code inside the function panics and the test will fail if the cut inside the function doesn't panic so there are a lot of cases inside of code where we want to check say the error handling and if the error handling isn't working properly maybe a bug is getting through then obviously we want to check that as well for instance if I turn this into a true statements or 3 or equals 3 then you'll see now that this test will now fail and there we go it says here tests test 3 failed and then we get our failures here all right now let's consider these examples so we've got multiple public functions here we have a add to function which just adds 2 to a another function here so another function called internal adder which just adds two numbers together then we've got a greeting function here which then just passes a reference to string inside of a format macro here and then returns it as a string and then we've got our test suite down here and then you'll see here that we've got this new line here that says use super and then of course we've got an asterisks after it so because we're inside of an inner module we need to bring the code under the test in the outer module into the scope of the inner module in order to run any of these functions we need to actually bring them inside of this module scope now of course we've chosen to use a glob here but you could specifically reference certain functions if you wanted to and the glob of course will bring in anything that we define up here we also have a new macro right here called assert EQ so assert EQ compares two arguments for equality and they'll print out the two values if this assertion fails so it's easier to see why the test failed in this case rather than just simply using assert the assert marker only tells us if we got a false value for the expression not the values that led to that false value whereas the assert EQ macro will show us the values that fail so this can be advantageous for us in specific cases we run our tests here and all of them pass perfectly fine so now let's make them fail so this puts in 4 and then this adds up to 4 so it returns 4 and 4 and if 4 and 4 equals 4 then certain EQ will pass let's make this five so it'll be five and four obviously this won't pass now so you can see here it says thread tests it works panic that assertion failed left equals right and then it says left is five and right is four and then it gives us the line in the code where the actual four came from so again this is pretty useful for checking for equality and allowing us to find out where exactly this error came from so in this case it's referencing the internal outer function up here telling us that this is not correct and that the five is also not correct we also have another macro called assert an e which does the exact opposite of assert EQ so this checks for inequality so in this case this will actually pass now because five is not equal to four here all right now consider this next example down here we are making a variable binding and we're calling our greeting function up here with the name Carroll inside of it and of course this returns a string that will say hello Carol and then we're going to assert that the result itself contains the name Carol inside of it this should return is true as hello Carol should have Carol in it but for instance if we make this a lowercase e it should then panic for us all right so you'll see here that it fails and it says that it does not contain Carol so we can also add a custom message to be printed out with the failure message as an optional argument to assert assert akyuu or assert any any arguments specified after the two required arguments to assert or the two required arguments to assert equ or assert any are passed along to the format macro and then converted to a string custom messages are obviously useful in order to document what the assertion means in the context of the code so that if the test fails we have a better idea of what a problem actually is all right so we've modified our greeting contains name test here and you can see here that we have a new set of arguments inside of our assert macro so we're still checking to see if the result contains Carol but if it does fail then we have this large error message that returns where it says greeting did not contain a name value was and then we can pass back the value itself so if we change for instance our result equal to max here then we will actually see that it will fail and then it'll say greeting did not contain the name value was max you can see here that we did in fact to get our error message where it said greeting did not contain name value was hello max so we can use this type of format to interpolate our string and then send back values that may not be strings and see what they actually look like inside of the test suite so we can also call specific tests so if we do not want to run the entire test suite we can just call one of our tests for instance say we just want to call greeting contains name we can then just run cargo tests and then specify the test that we want to run but just ran the one test greeting contains name and of course this still failed because we have max in there instead of Carol all right so let's talk a little bit more about this conditional compilation attribute so in this case our conditional compilation will only compile the code inside of this little module if we run our cargo test file so if we were to compile our application normally it'll just leave out this code here that way we don't have to worry about our tests slowing down the size of our runtime we can use this feature in other cases we have a conditional compilation attribute here and we're saying if target arrest equals Linux then this function will be compiled and it will get run so if our target OS is not Linux in this case and this function will be compiled and then in our main function here we're saying first run ru on Linux and this will choose the function that will run based on the actual target os that we're on so because we're on Windows this function here will not actually get compiled at all rather than having to worry about the two functions having the same name we can just leave this out by using this CFG attribute and then we also have a CFG macro here we're saying target OS equals Linux and we want to print this else otherwise we want to print this out in this case this entire thing will still compile but it will automatically tell us that we're not on so you see here we run it and you see here it says not running Linux check on us again not Linux now this type of feature is extremely powerful and it can be extremely useful for command-line tools imagine you want to make a command-line tool that works with a Windows operating system and also works on a UNIX operating system obviously the file systems are different on the two systems UNIX has various tools that Windows does not and they both have tools that have various different names as a result of this we can use our CFG here to make it so that we have different code that runs on each operating system and we can make sure for instance that the UNIX code will not compile on the Windows system and the windows code will not compile on the UNIX system which will ultimately make our program faster so we also have other various attributes for instance we'd have this allow attribute and we can pass in dead code this function obviously is useless but we don't want the win through to come out and say it's useless in this case so we can pass this in to control how our linter actually treats our code you'll see here they're running this we do not get any warnings back to say that the functions I'm team doesn't do anything whereas if we were to remove that attribute we would actually get some warnings so there are many other attributes inside of routes that we can gain access to and we'll talk about the attributes as we go into our projects from here on alright guys well I hope you enjoyed this tutorial if you did feel free to subscribe and like if you have any questions or comments feel free to leave them in the comment box below and if you disliked it then by all means down berta as much as you like have a good day
Original Description
In this video, we finish our intro to rust series by talking about the test system in Rust, the meta-data attributes, the configuration attribute and conditional compilation.
Feel free to donate:
BTC: 1ExBSiaEa3pceW98eptJwzR9QHrYZ71Xit
ETH: 0xD210Ea51F1615794A16080A108d2BC5471F60166
Check out our Twitter: https://twitter.com/TensorProgram
Check out our Facebook: https://www.facebook.com/Tensor-Programming-1197847143611799/
Check out our Steemit: https://steemit.com/@tensor
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tensor Programming · Tensor Programming · 55 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
▶
56
57
58
59
60
NodeJs, Text editors and IDEs
Tensor Programming
Vanilla JS todo App
Tensor Programming
Elm Tutorial part 1
Tensor Programming
Elm Lang Tutorial, Part 2
Tensor Programming
Elm Tutorial Part 3
Tensor Programming
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
Elm Tutorial part 5 -- Snake Game
Tensor Programming
Elm Tutorial part 6 -- Calculator
Tensor Programming
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
Go Tutorial part 2 -- Web Crawler
Tensor Programming
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
Go tutorial part 5 (web app part 3)
Tensor Programming
Go tutorial part 6 (webapp part 4)
Tensor Programming
Go tutorial part 7 (web app part 5)
Tensor Programming
Go tutorial part 8 (Web app part 6)
Tensor Programming
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
Go tutorial Part 10 (web app part 8)
Tensor Programming
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
Looking at Elm 0.18
Tensor Programming
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
Go tutorial part 16 (web app part 14)
Tensor Programming
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
Electron Elm Tutorial
Tensor Programming
Go tutorial part 17 (web app part 15)
Tensor Programming
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
elixir tutorial part 1
Tensor Programming
elixir tutorial part 2
Tensor Programming
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
A Intro to Clojure and Clojure Syntax
Tensor Programming
An Update about the channel
Tensor Programming
Intro to Rustlang (Setup and Primitives)
Tensor Programming
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
Intro to RustLang (Enums and Options)
Tensor Programming
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
Rustlang Project: Snake Game
Tensor Programming
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
Intro to Rust-lang (Error Handling)
Tensor Programming
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
Rustlang Project: Port Sniffer CLI
Tensor Programming
Rustlang Project: Chat Application
Tensor Programming
Rustlang Project: CLI Toy Blockchain
Tensor Programming
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming
More on: ML Maths Basics
View skill →Related Reads
📰
📰
📰
📰
Mastering Statistics for Machine Learning: From Core Fundamentals to Advanced Concepts
Medium · Machine Learning
Mastering Statistics for Machine Learning: From Core Fundamentals to Advanced Concepts
Medium · Deep Learning
Mastering Statistics for Machine Learning: From Core Fundamentals to Advanced Concepts
Medium · LLM
Graph-Native Reinforcement Learning Enables Traceable Scientific Hypothesis Generation through Conceptual Recombination
ArXiv cs.AI
🎓
Tutor Explanation
DeepCamp AI