Online Rust Playground
LabEx offers an Online Rust Playground, a cloud-based environment that allows you to quickly set up a Rust development environment for learning and experimentation.
Rust
Your First Rust Lab
Hi there, welcome to LabEx! In this first lab, you'll learn the classic 'Hello, World!' program in Rust.
Rust
Implement Generic Double Deallocation Trait
In this lab, a generic DoubleDrop trait is defined, which includes a double_drop method that allows a type to deallocate itself and an input parameter.
Rust
Exploring Rust's Associated Types
In this lab, we explore the concept of associated types in Rust, which allows for improved readability of code by defining inner types locally within a trait as output types. This is achieved by using the type keyword within the trait definition. with associated types, functions that use the trait no longer need to explicitly express the types A and B, making the code more concise and flexible. We rewrite an example from a previous section using associated types to illustrate their usage in practice.
Rust
Testcase: Unit Clarification
In this lab, the implementation of the Add trait with a phantom type parameter is examined using the example code provided in Rust.
Rust
Rust Multiple Bounds Exploration
In this lab, the code demonstrates the concept of multiple bounds in Rust, where a single type can have multiple bounds applied using the + operator.
Rust
New Type Idiom
In this lab, we explore the newtype idiom, which provides compile-time guarantees by allowing us to create a new type that is distinct from its underlying type. An example is shown where a struct Years is used to represent age in years, and a struct Days is used to represent age in days. By using the newtype idiom, we can ensure that the right type of value is supplied to a program, such as in the age verification function old_enough, which requires a value of type Years. Additionally, we learn how to obtain the value of a newtype as its underlying type using tuple or destructuring syntax.
Rust
Generic Container Trait Implementation
In this lab, we have a trait called Contains that is generic over its container type and requires explicit specification of its generic types when implemented for the Container type.
Rust
Defining Generic Functions in Rust
In this lab, you will learn how to define generic functions in Rust. To make a function generic, you need to prepend the type T with <T>. Sometimes, you may need to explicitly specify type parameters while calling a generic function, which can be done using the syntax fun::<A, B, ...>(). The code provided demonstrates the usage of generic functions in Rust and includes examples of functions that are both generic and non-generic.
Rust
Phantom Type Parameters
In this lab, we explore the concept of phantom type parameters, which are type parameters that are checked statically at compile time and do not have any runtime behavior or values. We demonstrate their usage in Rust by combining std::marker::PhantomData with the concept of phantom type parameters to create tuples and structs that contain different data types.
Rust
Testcase: Empty Bounds
In this lab, the code showcases how traits can be used as bounds, even if the traits do not include any functionality, by using the Eq and Copy traits as examples.
Rust
Expressive Rust Generics with Where Clause
In this lab, we learn that a where clause in Rust can be used to express bounds for generic types separately from their declaration, allowing for clearer syntax, and can also apply bounds to arbitrary types rather than just type parameters. The where clause is especially useful when the bounds are more expressive than the normal syntax, as shown in the example involving the PrintInOption trait.
Rust
Implementing Generic Types in Rust
In this lab, we learn how to implement generic types and methods in Rust, allowing us to specify different type parameters when using the struct or calling the methods.
Rust
Rust Generics Type Constraints
In this lab, when working with generics in Rust, type parameters must be bounded by traits to specify the functionality a type must implement.
Rust
Disabling Rust Unused Code Warnings
In this lab, the compiler provides a dead_code lint that warns about unused functions, but you can use attributes, such as #[allow(dead_code)], to disable the lint and prevent the warnings.
Rust
Conditional Rust Function Compilation
In this lab, we have a code snippet that includes a function called conditional_function(), but it will only be compiled and executed if a custom conditional called some_condition is passed to rustc using the --cfg flag.
Rust
Rust Software Testing Fundamentals
In this lab, we will explore the importance of testing in software development using Rust and how to write different types of tests such as unit tests and integration tests. We will also learn about organizing tests in Rust projects and running them using the cargo test command. Additionally, we will discuss the potential issues that can arise from running tests concurrently and provide an example to illustrate this.
Rust
Exploring Rust Generics Functionality
In this lab, we will be exploring the topic of generics, which involves generalizing types and functionalities to handle a broader range of cases, reducing code duplication. The syntax for generics in Rust involves specifying type parameters using angle brackets, such as <T>. By using generics, we can create generic functions that can accept arguments of any type. Furthermore, we can define generic types that can work with different concrete types.
Rust