Rust crate root

Created By: chatGPT

Rust crates are packages of Rust code. To create a new crate, we use the cargo command-line tool. The entry point of any Rust application is the main.rs file, and it typically resides in the src directory. The code within this file serves as the starting point for the execution of your application.
cargo new my_crate
cd my_crate

When you run cargo new, it creates a new directory with the following structure:

  • Cargo.toml: A manifest file that contains metadata about your project, such as dependencies and package information.
  • src/main.rs: The main source file where your Rust code will be written.

Here's an example content of main.rs:

fn main() {
    println!("Hello, world!");
}

You can build and run your crate using:

  • cargo build: Compiles the crate, generating an executable in the target/debug directory.
  • cargo run: Compiles and runs your crate in one command.
cargo build
cargo run
To add dependencies, simply edit the Cargo.toml file and add them under the [dependencies] section. For example, to add the Rand crate for random number generation, include it like this:
[dependencies]
rand = "0.8"
After adding the dependency, run cargo build again to fetch and compile the new dependency. You can then use it in your main.rs file, like so:
use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    let n: u32 = rng.gen_range(1..100);
    println!("Random number: {}", n);
}
Introduction And SetupVariablesData TypesImmutableMutableIntegerFloating PointBooleanCharacterStringArrayTupleVectorSliceHashmapMethodFunctionSyntaxBlock ExpressionIf ExpressionLoopWhile LoopFor LoopMatch ExpressionPattern MatchingOwnershipBorrowingReferencesLifetimesEnumsStructsTraitsImpl BlockGenericType AliasPanicResultOptionError HandlingUnwrappingVariantClosureIteratorAsyncAwaitTrait ObjectModuleCrateAttributeMacroCommentSingle Line CommentMulti Line CommentDoc CommentCargoFormattingOwnership RulesType InferenceShadowingOperatorArithmetic OperatorComparison OperatorLogical OperatorBitwise OperatorAs KeywordConstStaticCopy TraitClone TraitUnsafe CodeFfiCargo ManagementTraits BoundsMatch ArmDerived TraitsClosure CaptureSplit_atIterFilterMapCollectFrom_iterTuple StructUnit TypeNaming ConventionsModule SystemVisibilityPrivatePublicCrate RootUnix Specific FeaturesWindows Specific Features