Open32
Rustやってみる
始めてみるのはかんたん
とりあえずここ一通りやってみる
extern crate minigrep;
12.3で↑がエラーになった
issueで言ってるように
vscodeのconfig.json作って
"rust.build_bin": "minigrep"
追加したらいけた
The Rust Programming Language 日本語版
やっと一通り読み終えた
cargo new hello_world --bin
所有権、わかったようなわからんような
あとでまた考えよう
スマートポインタ周りもかな
スタックのみのデータ-コピー
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"
let a = [1, 2, 3, 4, 5];
println!("{:?}", a);
これで配列表示できる
let a = String::from("hello");
{
let b = a;
} // ここで確保したメモリが開放される?
aに確保したメモリは、所有権がbに移ってbのスコープが終わるタイミングで片付けられるのかな?
とりあえず、cliをcargo runしてみたら動いた
クレート周りがいまいちよくわかってない
externとか
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!!");
}
}
モジュールについて
- modはモジュールを定義しモジュールツリーに追加する
- モジュール内のpubを付けた物だけ公開される
- クレートルートはどうディレクトリ内のファイルをファイルと同名のモジュールとして使用できる
- modしないとモジュールツリーに追加されない
- ファイルと同名のディレクトリを作ることで子モジュールを別ファイルに定義できる
- モジュール定義はmodで行う
- モジュールツリー内のモジュールをcrateで絶対パス、self,superで相対パスで使用できる
crate::module1::function1(); // 絶対パス
self::module2::function2(); // 相対パス
- useモジュール内の要素が自分のモジュール内にあるかのようにパスを省略できる
use crate::module1;
- modでモジュールツリーに追加されていない場合useできない\
- 各モジュールのcrateは自分が追加されているクレートのモジュールツリーのルート
- なのでmodでクレートルートにつながってないと使えない
な感じ?
みたやつ