Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5782

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T12:28:07+00:00 2024-11-27T12:28:07+00:00

Rust Cheatsheet

  • 60k

Basics

1. Hello World

fn main() {     println!("Hello, world!"); } 
Enter fullscreen mode Exit fullscreen mode

2. Variables and Mutability

// Immutable variable let x = 5;  // Mutable variable let mut y = 10; y = 15; // OK  // Constants const MAX_POINTS: u32 = 100_000; 
Enter fullscreen mode Exit fullscreen mode

3. Data Types

// Scalar Types let num: i32 = -10; let floating: f64 = 3.14; let is_true: bool = true; let character: char = 'a';  // Compound Types let tuple: (i32, f64, bool) = (500, 6.4, true); let array: [i32; 5] = [1, 2, 3, 4, 5]; 
Enter fullscreen mode Exit fullscreen mode

Control Flow

1. If Statement

let number = 6;  if number % 2 == 0 {     println!("Even"); } else {     println!("Odd"); } 
Enter fullscreen mode Exit fullscreen mode

2. Loop

let mut counter = 0;  loop {     counter += 1;      if counter == 5 {         break;     } } 
Enter fullscreen mode Exit fullscreen mode

3. While Loop

let mut countdown = 5;  while countdown > 0 {     println!("{}", countdown);     countdown -= 1; } 
Enter fullscreen mode Exit fullscreen mode

4. For Loop

let numbers = [1, 2, 3, 4, 5];  for num in numbers.iter() {     println!("{}", num); } 
Enter fullscreen mode Exit fullscreen mode

Functions

1. Basic Function

fn greet(name: &str) {     println!("Hello, {}!", name); }  greet("Ali"); 
Enter fullscreen mode Exit fullscreen mode

2. Functions with Return Values

fn add(a: i32, b: i32) -> i32 {     a + b }  let result = add(5, 3); 
Enter fullscreen mode Exit fullscreen mode

Error Handling

1. Result Type

fn divide(a: f64, b: f64) -> Result<f64, &'static str> {     if b == 0.0 {         return Err("Division by zero");     }     Ok(a / b) }  match divide(10.0, 2.0) {     Ok(result) => println!("Result: {}", result),     Err(err) => println!("Error: {}", err), } 
Enter fullscreen mode Exit fullscreen mode

Ownership, Borrowing, and Lifetimes

1. Ownership

let s1 = String::from("hello"); let s2 = s1; // Ownership moved to s2 
Enter fullscreen mode Exit fullscreen mode

2. Borrowing

fn calculate_length(s: &String) -> usize {     s.len() }  let s = String::from("hello"); let len = calculate_length(&s); // Borrowed s 
Enter fullscreen mode Exit fullscreen mode

3. Lifetimes

fn longest<'a>(s1: &'a str, s2: &'a str) -> &'a str {     if s1.len() > s2.len() {         s1     } else {         s2     } } 
Enter fullscreen mode Exit fullscreen mode

Advanced Concepts

1. Structs

struct Rectangle {     width: u32,     height: u32, }  impl Rectangle {     fn area(&self) -> u32 {         self.width * self.height     } }  let rect = Rectangle { width: 10, height: 20 }; let area = rect.area(); 
Enter fullscreen mode Exit fullscreen mode

2. Enums

enum Coin {     Penny,     Nickel,     Dime,     Quarter, }  fn value_in_cents(coin: Coin) -> u8 {     match coin {         Coin::Penny => 1,         Coin::Nickel => 5,         Coin::Dime => 10,         Coin::Quarter => 25,     } } 
Enter fullscreen mode Exit fullscreen mode

3. Traits

trait Vehicle {     fn drive(&self); }  struct Car;  impl Vehicle for Car {     fn drive(&self) {         println!("Car is driving...");     } }  let car = Car; car.drive(); 
Enter fullscreen mode Exit fullscreen mode

Conclusion

This Rust cheatsheet provides a quick overview of essential concepts and syntax in Rust programming language, catering to beginners and intermediate developers alike. Whether you're building a small utility or a complex system, Rust's safety features and performance make it an excellent choice for various applications.

programmingrustsoftwareengineeringwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.