Rust:引用如何成为类型?

时间:2019-03-05 05:03:55

标签: reference rust

所以我要问的是,返回类型&std :: vec :: Vec和std :: vec :: Vec之间有什么区别?只是好奇。如果我引用某物,则不是在创建新类型。它仍然保留其数据和结构,因此保留其类型。但是由于某种原因,我得到了这个错误:

   error[E0308]: mismatched types
   --> src/cam.rs:170:3
    |
168 |     pub fn index2d(self, x: usize, y: usize) -> Vec<u8> {
    |                                                 ------- expected `std::vec::Vec<u8>` because of return type
169 |         let c = &self.pyxels[y*WIDTH+x];
170 |         c
    |         ^
    |         |
    |         expected struct `std::vec::Vec`, found reference
    |         help: try using a conversion method: `c.to_vec()`
    |
    = note: expected type `std::vec::Vec<u8>`
               found type `&std::vec::Vec<u8>`
                           ^ (umm excuse me?)

那个小符号(&)似乎确实起到了所有作用,我不知道为什么。

1 个答案:

答案 0 :(得分:4)

  

如果我引用某物,则不是在创建新类型。

如果通过“创建新类型” 来表示“创建其他类型的对象” ,那么是的,这就是您正在做的事情。对事物的引用不是那个事物。这类似于拥有房子和纸条上写有房子的地址。

尽管Rust的语法通常使通过引用访问对象看起来与直接访问该对象相同。

New input system (experimental) initialized
Receiving unhandled NULL exception
Obtained 32 stack frames.
#0 0x0000010089e0cf in MonoManager::GetMonoClassWithAssemblyName(core::basic_string<char, core::StringStorageDefault<char> > const&, core::basic_string<char, core::StringStorageDefault<char> > const&, core::basic_string<char, core::StringStorageDefault<char> > const&)
#1 0x000001008c2850 in scripting_class_from_fullname(char const*, char const*, char const*)
#2 0x000001008aa2d0 in RequireMethod(char const*, char const*, char const*, char const*)
#3 0x0000010031cfcf in GetInputScriptingClassesPtr()
#4 0x0000010031cda9 in GetInputScriptingClasses()
#5 0x00000100d82415 in InputUpdate(InputUpdateType)
#6 0x000001003969e9 in InputInit()
#7 0x0000010037d8a0 in SetupUnityPlayer(bool, int, char**)
#8 0x000001003804ab in -[PlayerAppDelegate applicationDidFinishLaunching:]
#9 0x007fff4b577632 in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
#10 0x007fff4b5775ac in ___CFXRegistrationPost_block_invoke
#11 0x007fff4b5774cd in _CFXRegistrationPost
#12 0x007fff4b57f929 in ___CFXNotificationPost_block_invoke
#13 0x007fff4b4e70ca in -[_CFXNotificationRegistrar find:eek:bject:eek:bserver:enumerator:]
#14 0x007fff4b4e648d in _CFXNotificationPost
#15 0x007fff4d86ea7b in -[NSNotificationCenter postNotificationName:eek:bject:userInfo:]
#16 0x007fff48a6b64a in -[NSApplication _postDidFinishNotification]
#17 0x007fff48a6af6e in -[NSApplication _sendFinishLaunchingNotification]
#18 0x007fff48a688c8 in -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:]
#19 0x007fff48a68517 in -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:]
#20 0x007fff4d8b9144 in -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:]
#21 0x007fff4d8b8fc0 in _NSAppleEventManagerGenericHandler
#22 0x007fff4c79eb93 in aeDispatchAppleEvent(AEDesc const*, AEDesc*, unsigned int, unsigned char*)
#23 0x007fff4c79e3fd in dispatchEventAndSendReply(AEDesc const*, AEDesc*)
#24 0x007fff4c79e2d5 in aeProcessAppleEvent
#25 0x007fff4a7b310e in AEProcessAppleEvent
#26 0x007fff48a64644 in _DPSNextEvent
#27 0x007fff48a63102 in -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:]
#28 0x007fff48a5d165 in -[NSApplication run]
#29 0x007fff48a4c8a3 in NSApplicationMain
#30 0x0000010037eab5 in PlayerMain(int, char const**)
#31 0x007fff78764ed9 in start
Stacktrace:


Native stacktrace:

0 libsystem_kernel.dylib 0x00007fff788a423e __pthread_kill + 10
1 libsystem_c.dylib 0x00007fff7880d1c9 abort + 127
2 Rainswept 0x000000010089fcb4 _Z12HandleSignaliP9__siginfoPv + 36
3 libmono.0.dylib 0x000000010daebe65 mono_chain_signal + 75
4 libmono.0.dylib 0x000000010da334a6 mono_sigsegv_signal_handler + 210
5 libsystem_platform.dylib 0x00007fff7894fb3d _sigtramp + 29
6 ??? 0x00007ffeefbfd360 0x0 + 140732920746848
7 Rainswept 0x00000001008c2850 _Z29scripting_class_from_fullnamePKcS0_S0_ + 240
8 Rainswept 0x00000001008aa2d0 _Z13RequireMethodPKcS0_S0_S0_ + 48
9 Rainswept 0x000000010031cfcf _Z27GetInputScriptingClassesPtrv + 143
10 Rainswept 0x000000010031cda9 _Z24GetInputScriptingClassesv + 9
11 Rainswept 0x0000000100d82415 _Z11InputUpdate15InputUpdateType + 21
12 Rainswept 0x00000001003969e9 _Z9InputInitv + 409
13 Rainswept 0x000000010037d8a0 _Z16SetupUnityPlayerbiPPc + 1200
14 Rainswept 0x00000001003804ab -[PlayerAppDelegate applicationDidFinishLaunching:] + 939
15 CoreFoundation 0x00007fff4b577632 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
16 CoreFoundation 0x00007fff4b5775ac ___CFXRegistrationPost_block_invoke + 63
17 CoreFoundation 0x00007fff4b5774cd _CFXRegistrationPost + 398
18 CoreFoundation 0x00007fff4b57f929 ___CFXNotificationPost_block_invoke + 87
19 CoreFoundation 0x00007fff4b4e70ca -[_CFXNotificationRegistrar find:eek:bject:eek:bserver:enumerator:] + 1633
20 CoreFoundation 0x00007fff4b4e648d _CFXNotificationPost + 742
21 Foundation 0x00007fff4d86ea7b -[NSNotificationCenter postNotificationName:eek:bject:userInfo:] + 66
22 AppKit 0x00007fff48a6b64a -[NSApplication _postDidFinishNotification] + 313
23 AppKit 0x00007fff48a6af6e -[NSApplication _sendFinishLaunchingNotification] + 209
24 AppKit 0x00007fff48a688c8 -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:] + 552
25 AppKit 0x00007fff48a68517 -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] + 690
26 Foundation 0x00007fff4d8b9144 -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 287
27 Foundation 0x00007fff4d8b8fc0 _NSAppleEventManagerGenericHandler + 102
28 AE 0x00007fff4c79eb93 _Z20aeDispatchAppleEventPK6AEDescPS_jPh + 1855
29 AE 0x00007fff4c79e3fd _ZL25dispatchEventAndSendReplyPK6AEDescPS_ + 41
30 AE 0x00007fff4c79e2d5 aeProcessAppleEvent + 439
31 HIToolbox 0x00007fff4a7b310e AEProcessAppleEvent + 55
32 AppKit 0x00007fff48a64644 _DPSNextEvent + 1734
33 AppKit 0x00007fff48a63102 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1362
34 AppKit 0x00007fff48a5d165 -[NSApplication run] + 699
35 AppKit 0x00007fff48a4c8a3 NSApplicationMain + 780
36 Rainswept 0x000000010037eab5 _Z10PlayerMainiPPKc + 949
37 libdyld.dylib 0x00007fff78764ed9 start + 1

Debug info from gdb:


=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================

那么也许这就是为什么你感到困惑?