2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rustチートシート

Last updated at Posted at 2022-12-20

Rustチートシート

  • あくまで自分用なので雑なのは許してください(学んだものから更新しているので統一性はありません…)
  • 2022/12/20 リアルタイムで勉強中なのでどんどん更新していきます

変数

  • ミュータブルとイミュータブル
    定義した変数に再代入したい時はmutをつける
    fn main(){
        let mut s1 = String::from("hell");
        s1 = String::from("hello");
        let s2 = ("world");
        let s3 = s1 + s2;
        println!("{}", s3);
    }
    

所有権

fn main(){
    let s1: String = String::from("Hello");
    let s2: &str = "World";
    println!("{}", add_string(s1, s2));
}

//sは変更したいのでミュータブルのString型で受け取る
//tは変更するう必要がないので参照だけを受け取る
fn add_string(s: String, t: &str) -> String{
    s + t
}

結合

文字列結合

  • 結合される側はString型、結合する側は文字列リテラル(結合される側は変更されるためミュータブルである必要があるが、結合する側は変更されないのでイミュータブルでよい)
    fn main(){
        let mut s1 = String::from("hello");
        let s2 = ("world");
        let s3 = s1 + s2;
        println!("{}", s3);
    }
    

構造体

  • 定義
    struct User {
        username: String,
        email: String,
        sign_in_count: u64,
        active: bool,
    }
    
  • 構造体メソッド定義
    impl User{
        //引数にselfを持たせないことでstaticメソッドとして定義できる
        pub fn new(username: String, email: String, sign_in_count: u64, active: bool) -> Self{
            Self{username: username, email: email, sign_in_count: sign_in_count, active: active}
        }
        //通常の構造体メソッド
        pub fn print_name(self){
            println!("{}", self.username);
        }
    }
    
  • メソッド呼び出し
    let user: User = User::new(String::from("revioness"), String::from("revioness.outlook.jp"), 1, true);
    user.print_name();
    
  • インスタンス化
    let user1 = User {
        email: String::from("someone@example.com"),
        username: String::from("someusername123"),
        active: true,
        sign_in_count: 1,
    };
    
  • 参照
    println!("email:{}", user1.email);
    
  • 値の変更
    let mut user1 = User {
        email: String::from("someone@example.com"),
        username: String::from("someusername123"),
        active: true,
        sign_in_count: 1,
    };
    
    今まで何度も言っているように、let 変数名で定義したものはイミュータブルになるので、値の変更ができない
    なのでmutをつけてミュータブルにする
    構造体のこの変数だけミュータブルに!といったことはできない
    user1.username = String::from("test123");
    
  • 省略形
    let mut user1 =  User {
                email: String::from("someone@example.com"),
                username: String::from("someusername123"),
                active: true,
                sign_in_count: 1,
            };
    let mut user2 =  User {
                email: String::from("someone2@example.com"),
                username: String::from("someusername1234"),
                ..user1
            };
    
    activesign_in_countがuser1と同じ値が入る

タプル

struct Color(i32, i32, i32);
struct Point(i32, i32, i32);

let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);

この場合、中の構造は同じだが、型はColor型とPoint型に分かれる

println!

  • そのまま出力
    println!("rect1 is {}", 1);
    
  • 構造体の出力
    struct Rectangle {
        width: u32,
        height: u32,
    }
    
    fn main() {
        let rect1 = Rectangle { width: 30, height: 50 };
    
        // rect1は{}です
        println!("rect1 is {:?}", rect1);
    }
    
    rect1 is Rectangle { width: 30, height: 50 }
    
    さらに見やすく
    struct Rectangle {
        width: u32,
        height: u32,
    }
    
    fn main() {
        let rect1 = Rectangle { width: 30, height: 50 };
    
        // rect1は{}です
        println!("rect1 is {:#?}", rect1);
    }
    
    rect1 is Rectangle {
        width: 30,
        height: 50,
    }
    

ファイル入出力

ファイル読み込み

use std::fs;
use std::error;
use std::io::Write;

//Stringで読み込み
let content = fs::read_to_string("ファイルパス")?;

//書き込み
let mut file = fs::File::create("ファイルパス")?;
file.write_all(buf).unwrap();

json

//Cargo.toml
[dependencies]
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"

//src
use serde::{Serialize, Deserialize};

//構造体で継承
#[derive(Serialize, Deserialize)]


let json = serde_json::to_string("json文字列").unwrap();

Logger

  • env_logger
    //Cargo.toml
    [dependencies]
    log = "0.4.0"
    env_logger = "0.10.0"
    
    //src
    use env_logger;
    use log::{error, warn, info, debug};
    
    //デバッグレベル設定・初期化
    env::set_var("RUST_LOG", "info");
    env_logger::init();
    
    info!("コメント");
    
    #出力
    [2022-12-29T14:42:24Z INFO  アプリ名] コメント
    
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?