Rust
cotrGenList
Rust Generate Array
let ${1:myList} = [ ${2:'item1'}, ${3:'item2'}, // Add more items here];
cotrFunc
Rust Function
fn ${2:my_function}(${3:parameters}) -> ${1:void} { ${4:// Your code here}}
cotrEnum
Rust Enum
enum ${1:MyEnum} { ${2:Variant1}, ${3:Variant2}, // Add more variants here}
cotrTypesBoolFalse
Rust Boolean False
false
cotrTypesChar
Rust Character Type
char
cotrNull
Rust Null Type (Option)
None
cotrTypesIntAlt
Rust Integer Type (i64)
i64
cotrAny
Rust Dynamic Type (Any)
dyn Any // There is no built-in Any type in Rust.
cotrNotEqual
Rust Not Equal To
!=
cotrTypeCheck
Rust Type Check
${1:variable}.type()
cotrTypesInt
Rust Integer Type (i32)
i32
cotrTypesList
Rust List Type
Vec<${1:Type}>
cotrPrintMulti
Rust Print Multi
println!("{}{}{}",${1:"Line 1"},${2:"Line 2"},${3:"Line 3"});
cotrFuncAnon
Rust Anonymous Function
|${1:parameters}| -> ${2:ReturnType} { ${3:// Your code here}}
cotrEqual
Rust Equal To
==
cotrTypesStringAlt
Rust String Type (&str)
&str
cotrTypesBool
Rust Boolean Type (bool)
bool
cotrComment
Rust Comment
// ${1:comment}
cotrIfElse
Rust If Else Statement
if ${1:condition} { ${2:// Your code here}} else if ${3:condition} { ${4:// Your code here}} else { ${5:// Your code here}}
cotrOperatorsBool
Rust Boolean Operators
// Rust Boolean Operators// Logical AND: &&// Logical OR: ||// Logical NOT: !// Equality: ==// Inequality: !=// Greater than: >// Less than: <// Greater than or equal to: >=// Less than or equal to: <=
cotrVarStringMulti
Rust Create Multi-line String Variable
let ${1:myStr} = String::from( ${2:'Line 1'} ${3:'Line 2'} ${4:'Line 3'});
cotrForOfLoop
Rust For…Of Loop
for ${1:item} in ${2:iterable} { // Your code here}
cotrVarList
Rust Create List Variable
let ${1:myList} = [ ${2:'item1'}, ${3:'item2'}, // Add more items here];
cotrFuncArgsNamed
Rust Function Named Args
// Note: Rust does not support named arguments in function definitions.
cotrInterpolate
Rust Interpolate String
format!("${1:Your message here}")
cotrVar
Rust Create Variable
let ${1:myVar} = $2;
cotrVarNum
Rust Create Float Variable
let ${1:myFloat} = ${2:0.0};
cotrVarBool
Rust Create Boolean Variable
let ${1:myBool} = ${2:true};
cotrConst
Rust Create Constant
const ${1:myConst}: ${2:type} = $3;
cotrStructure
Rust Project Structure (High-Level)
// Recommended High-Level Rust Project Structure:
// - Cargo.toml: The manifest file for Rust’s package manager, Cargo.// - src/// - lib.rs: The entry point for library crates.// - main.rs: The entry point for binary crates (executable applications).// - /bin/: Additional binaries can be placed here as separate .rs files.// - /modules/: Organize code by feature in different modules (files or directories).
// - tests/// - Integration tests go here, in separate files or directories.
// - examples/// - Example programs demonstrating the use of the crate.
// - benches/// - Benchmarks go here, using the built-in benchmarking support in Rust.
// - target/// - Generated by Cargo, contains compilation artifacts and executables.
// - .cargo/// - Configuration for Cargo can be placed here.
// Note:// - This structure supports both library and executable projects.// - Use Cargo workspaces for multi-crate projects.
cotrTypesNum
Rust Float Type (f32)
f32
cotrWhileLoop
Rust While Loop
while ${1:condition} { // Your code here}
cotrCommentMulti
Rust Multi-line Comment
/* * ${1:comment} */
cotrSwitch
Rust Match Statement
match ${1:variable} { ${2:value1} => { // Your code here }, ${3:value2} => { // Your code here }, _ => { // Your code here }}
cotrTypes
Rust Types
$BLOCK_COMMENT_STARTRust is a statically typed language.
Types in Rust include:- i8, i16, i32, i64, i128: Signed integers- u8, u16, u32, u64, u128: Unsigned integers- f32, f64: Floating-point numbers- bool: Boolean- char: Character- str: String slice- String: Heap-allocated string- &[T]: Slice of type T- Vec<T>: Vector of type T- Option<T>: Optional value of type T- Result<T, E>: Result of a computation that could be successful (T) or an error (E)- Box<T>: Boxed (heap-allocated) type T- Rc<T>: Reference-counted type T- RefCell<T>: Mutable memory location with dynamically checked borrow rules- HashMap<K, V>: Hash map with key type K and value type V- Tuple (T1, T2, ...): Tuple with elements of different types- Array [T; N]: Array with elements of type T and fixed size N- Struct: Custom data type with named fields- Enum: Custom data type with variants$BLOCK_COMMENT_END
cotrVarSyntax
Variable Declaration Syntax
// Rust Variable Declaration Syntax:
// - let: (Scope: Block)// - Declares an immutable variable by default.// - Use for values that should not change.
// - let mut: (Scope: Block)// - Declares a mutable variable.// - Use for variables that need to be reassigned.
// - const: (Scope: Global)// - Declares a constant value.// - Must be initialized with a constant expression.
// Note:// - Rust does not have a direct equivalent to 'var'.// - Use 'let' for most variable declarations.// - Use 'let mut' for variables that need to be reassigned.// - Use 'const' for values that are known at compile time.
cotrVarDate
Rust Create Date Variable
// Rust does not have a built-in Date type.// Use the chrono crate to create a date variable.use chrono::{DateTime, Utc};
let ${1:my_date}: DateTime<Utc> = Utc::now();
cotrInfo
Rust Info
Typing: Statically typedParadigm: Multi-paradigm: imperative, functional, concurrentCompilation: CompiledConcurrency: Built-in support for concurrency with ownership and borrowing, and asynchronous programming with async/await
cotrTryCatch
Rust Try Catch
let result = std::panic::catch_unwind(|| { ${1:// Your code here}});if let Err(err) = result { ${2:// Your code here}}
cotrTypesDate
Rust Date Type
// Rust does not have a built-in Date type.// Use the chrono crate for date and time operations.use chrono::{DateTime, Utc};
cotrConcat
Rust Concatenate String
format!("${1:Your message here} ${2:and} ${3:more}")
cotrClosure
Rust Closure
let ${1:my_closure} = |${2:parameters}| ${3:expression};
cotrEntry
Rust Entry Point
// Rust Entry Point// To run this program, use: `cargo run`// Note: This assumes you are using Cargo, Rust's package manager and build tool.
fn main() { println!("Hello, World!");}
cotrOperators
Rust Mathematical Operators
// Rust Mathematical Operators// Addition: +// Subtraction: -// Multiplication: *// Division: /// Modulus (Remainder): %// Exponentiation: Not directly supported, use `f32::powi` or `f64::powf` for floating-point numbers// Increment: Not directly supported, use `+= 1`// Decrement: Not directly supported, use `-= 1`// Assignment: =// Addition assignment: +=// Subtraction assignment: -=// Multiplication assignment: *=// Division assignment: /=// Modulus assignment: %=
cotrFuncSyntax
Rust Function Syntax
// Rust Function Syntax// Basic function: fn function_name(parameters) -> return_type { ... }// Function with arguments: fn function_name(arg1: type1, arg2: type2, ...) -> return_type { ... }// Note: Rust does not support named arguments in function definitions.
cotrClass
Rust Struct
struct ${1:MyStruct} { ${2:field1}: ${3:type1}, ${4:field2}: ${5:type2}, // Add more fields here}
cotrThrow
Rust Throw Exception
panic!("Your message here")
cotrIf
Rust If Statement
if ${1:condition} { ${2:// Your code here}}
cotrTypesMap
Rust Map Type
std::collections::HashMap<${1:KeyType}, ${2:ValueType}>
cotrVarMapAlt
Rust Create Map Variable (BTreeMap)
use std::collections::BTreeMap;
let ${1:myMap} = BTreeMap::new();
cotrPrint
Rust Print
println!("{${1:'Your message here'}}");
cotrVarTyped
Rust Create Typed Variable
let ${1:my_var}: ${2:Type} = $3;
cotrForLoop
Rust For Loop
for ${1:i} in ${2:0..10} { // Your code here}
cotrTypesString
Rust String Type (String)
String
cotrTypesNumAlt
Rust Float Type (f64)
f64
cotrNow
Rust Date Now
// Rust does not have a built-in Date type.// Use the chrono crate to get the current date and time.use chrono::Utc;
Utc::now()
cotrTypeCompare
Rust Type Comparison
// Check if two variables have the same type:if ${1:variable1}.type_id() == ${2:variable2}.type_id() { // Your code here}
cotrVarInt
Rust Create Integer Variable
let ${1:myInt} = ${2:0};
cotrVarMap
Rust Create Map Variable (Hashmap)
use std::collections::HashMap;
let ${1:myMap} = HashMap::new();
cotrTypeConvert
Rust Type Conversion
// Rust Type Conversion:
// Rust emphasizes explicit type conversions for safety.
// Explicit conversions:// - variable as TargetType // Type casting (can panic if conversion fails)// - TargetType::from(variable) // Conversion methods (e.g., i32::from(f64))
// Note:// - Be cautious with type casting, as it can lead to panics if the conversion is not valid.
cotrGenMap
Rust Generate Object Map
let ${1:myMap} = [ ${2:'key1'}: ${3:'value1'}, ${4:'key2'}: ${5:'value2'}, // Add more key-value pairs here];
cotrMutVar
Rust Create Mutable Variable
let mut ${1:myVar} = $2;
cotrFuncArgs
Rust Function Args
// In Rust, default arguments are not supported but you can set default values using patterns like Option<T> or custom types.fn ${2:my_function}(${3:arg1}: ${4:type1}, ${5:arg2}: Option<${6:type2}> = None) -> ${1:void} { ${7:// Your code here}}
cotrTernary
Rust Ternary Operator
${1:condition} ? ${2:trueValue} : ${3:falseValue}
cotrTypesBoolTrue
Rust Boolean True
true
cotrVarNullable
Rust Create Nullable Variable
let ${1:my_var}: Option<${2:Type}> = $3;
cotrVarString
Rust Create String Variable
let ${1:myStr} = String::from(${2:"myString"});