Open32

Rustやってみる

kamodakarakamodakara

https://doc.rust-jp.rs/book-ja/title-page.html
とりあえずここ一通りやってみる

Hidden comment
Hidden comment
kamodakarakamodakara

所有権、わかったようなわからんような
あとでまた考えよう

Hidden comment
kamodakarakamodakara
fn main() {
    let test1 = String::from("hello");
    println!("[new] test1 <= {:p} << \"{}\"", test1.as_ptr(), test1);
    println!("test1     | {:p} \"{}\"", test1.as_ptr(), test1);
    let mut test2 = String::from("world");
    println!("[new] test2 <= {:p} << \"{}\"", test2.as_ptr(), test2);
    println!("test2     | {:p} \"{}\"", test2.as_ptr(), test2);
    println!("[ムーブ] test2_back <= test2({:?})", test2.as_ptr());
    let test2_back = test2; // ムーブ
    println!("test2_back| {:p} \"{}\"", test2_back.as_ptr(), test2_back);
    println!("[ムーブ] test2 <= test1({:?})", test1.as_ptr());
    test2 = test1; // ムーブ
    println!("test2     | {:p} \"{}\"", test2.as_ptr(), test2);
    test2.push_str(" world");
    println!("test2 = test2 + \" world\"");
    println!("test2     | {:p} \"{}\"", test2.as_ptr(), test2);
    let test3 = &mut test2; // 借用
    println!("[借用] test3 <- test2");
    println!("test3     | {:p} \"{}\"", test3.as_ptr(), test3);
    let test4 = &test3; // 借用
    println!("[借用] test4 <- test3");
    println!("test4     | {:p} \"{}\"", test4.as_ptr(), test4);
    println!("test3     | {:p} \"{}\"", test3.as_ptr(), test3);
    test3.push_str("!!!!");
    println!("test3 = test3 + \"!!!!\"");
    // println!("test4     | {:p} \"{}\"", test4.as_ptr(), test4);
    println!("test3     | {:p} \"{}\"", test3.as_ptr(), test3);
    println!("test2     | {:p} \"{}\"", test2.as_ptr(), test2);
    test2 = String::from("The World");
    println!("[new] test2 <= {:p} << \"{}\"", test2.as_ptr(), test2);

    // println!("test1     | {:p} \"{}\"", test1.as_ptr(), test1);
    println!("test2     | {:p} \"{}\"", test2.as_ptr(), test2);
    println!("test2_back| {:p} \"{}\"", test2_back.as_ptr(), test2_back);
}

出力

[new] test1 <= 0x55b4b9748ad0 << "hello"
test1     | 0x55b4b9748ad0 "hello"
[new] test2 <= 0x55b4b9748af0 << "world"
test2     | 0x55b4b9748af0 "world"
[ムーブ] test2_back <= test2(0x55b4b9748af0)
test2_back| 0x55b4b9748af0 "world"
[ムーブ] test2 <= test1(0x55b4b9748ad0)
test2     | 0x55b4b9748ad0 "hello"
test2 = test2 + " world"
test2     | 0x55b4b9748ad0 "hello world"
[借用] test3 <- test2
test3     | 0x55b4b9748ad0 "hello world"
[借用] test4 <- test3
test4     | 0x55b4b9748ad0 "hello world"
test3     | 0x55b4b9748ad0 "hello world"
test3 = test3 + "!!!!"
test3     | 0x55b4b9748ad0 "hello world!!!!"
test2     | 0x55b4b9748ad0 "hello world!!!!"
[new] test2 <= 0x55b4b9748b10 << "The World"
test2     | 0x55b4b9748b10 "The World"
test2_back| 0x55b4b9748af0 "world"
kamodakarakamodakara
    let a = String::from("hello");
    {
        let b = a;
    } // ここで確保したメモリが開放される?

aに確保したメモリは、所有権がbに移ってbのスコープが終わるタイミングで片付けられるのかな?

kamodakarakamodakara

クレート周りがいまいちよくわかってない
externとか

kamodakarakamodakara
pub fn test_sub_function() {
  println!("test");
  test_sub_sub::test_sub_sub_sub::test_function();
}


pub mod test_sub_sub {
  pub mod test_sub_sub_sub {
    pub fn test_function() {
      println!("test");
      super::super::test_sub_function();
    }
  }
  pub fn test_function() {
    test_sub_sub_sub::test_function();
    super::test_sub_function();
    super::test_sub2::sub2_function();
  }
}

mod test_sub2 {
  pub fn sub2_function() {
    println!("sub2 fn!!");
  }
}
kamodakarakamodakara

モジュールについて

  • modはモジュールを定義しモジュールツリーに追加する
  • モジュール内のpubを付けた物だけ公開される
  • クレートルートはどうディレクトリ内のファイルをファイルと同名のモジュールとして使用できる
    • modしないとモジュールツリーに追加されない
  • ファイルと同名のディレクトリを作ることで子モジュールを別ファイルに定義できる
    • モジュール定義はmodで行う
  • モジュールツリー内のモジュールをcrateで絶対パス、self,superで相対パスで使用できる
crate::module1::function1(); // 絶対パス
self::module2::function2(); // 相対パス
  • useモジュール内の要素が自分のモジュール内にあるかのようにパスを省略できる
use crate::module1;
  • modでモジュールツリーに追加されていない場合useできない\
  • 各モジュールのcrateは自分が追加されているクレートのモジュールツリーのルート
    • なのでmodでクレートルートにつながってないと使えない

な感じ?
みたやつ
https://doc.rust-jp.rs/book-ja/ch07-01-packages-and-crates.html
https://keens.github.io/blog/2018/12/08/rustnomoju_runotsukaikata_2018_editionhan/
https://qiita.com/kerupani129/items/bd41d8a07daa31256aab
https://dackdive.hateblo.jp/entry/2020/12/09/103000